JBoss-IDE 1.2.2 Tutorial 3
Servlet and Web-APP
Only EJB is still not enough, we have to write a servlet to calculate the EJB to calculate the number of Fi Ponacon.
Add a Class in the project, Package Enter 'Tutorial.Web' and the name Enter to 'ComputeServlet'. Press' Browse ... 'Add' httpservlet 'in SuperClass'. Select' Constructors from Superclass' and 'Inherited Abstract Methods'.
The following screen should be seen after 'finish'.
Open 'ComputeServlet.java' file. Right click on the right hand window selection 'source' -> 'override / import method'. The 'init' and the 'dopost' method is then pressing 'OK', which is automatically generated.
Plus two private members: private fibohome home;
PRIVATE STRING VALUE;
The definition 'init' method is as follows, this is initializing the EJB HOME interface: public void init () throws servletexception {
Try {
Context context = new initialContext ();
Value = (string) Context.lookup ("java: / comp / env / title);
Object ref = context.lookup ("java: / comp / ENV / EJB / FIBO");
Home = (FiboHome) PortableremoteObject.narrow (Ref, FiboHome.Class);
} catch (exception e) {
Throw New ServleTexception ("Lookup of Java: / Comp / Env / Failed");
}
}
Definition 'dopost' method is as follows, this is a 'LIMIT' requirement, add a new EJB instance to output the result to HTML: protected void dopost (httpservletRequest request, httpservletresponse response) throws servleTexception, ioException {
Response.setContentType (Text / HTML ");
PrintWriter out = response.getwriter ();
Out.println ("
Out.println (Value);
Out.println (" Title> HEAD>");
Out.println ("
Out.println ("
Out.println (Value);
OUT.PRINTLN (" h1>");
Try {
Fibo bean = home.create ();
INT LIMIT = 0;
String value = Request.getParameter ("limit");
IF (value! = null) {
Try {
Limit = integer.parseint (value);
} catch (exception e) {}
}
Double [] result = bean.compute (limited);
Bean.remove (); OUT.PRINTLN ("
");
Out.print ("THE");
Out.print (Limit);
Out.print ("First Fibonacci Numbers);
For (int i = 0; i Out.println (" Out.println (i); OUT.PRINTLN (":"); Out.println (Result [i]); } Out.println (" p>"); } Catch (Exception E) { Out.println (E.GetMessage ()); E.PrintStackTrace (OUT); } Finally { Out.println (" Body> HTML>"); Out.close (); } } The next step is to insert a servlet xdoclet javadoc-related label. Write '@Web' in Javadoc. The automatic completion function is written in Javadoc. The other properties is also the same, the code is as follows: * * @ Web.Servlet Name = "ComputeServlet" * Display-name = "computation servlet" * Description = "Servlet That Compute Fibonacci Suite" * * @ Web.Servlet-maping url-pattern = "/ compute" * * @ Web.env-entry name = "title" * Type = "java.lang.string" * Value = "Fibonacci Computation" * Description = "Example of Env Entry" * * @ Web.ejb-ref name = "EJB / FIBO" * Type = "session" * Home = "tutorial.interfaces.fibohome" * Remote = "Tutorial.Interfaces.fibo" * Description = "Reference to the fibo ejb" * * @ jboss.ejb-ref-jndi ref-name = "EJB / FIBO" * JNDI-NAME = "EJB / TUTORALIAL / FIBO" * / Public class computeServlet Extends httpservlet {
");