JBoss-IDE 1.2.2 Tutorial 3
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 { JBoss-IDE 1.2.2 Tutorial 4 JBoss-IDE 1.2.2 Tutorial 4: The complete code of 'ComputeServlet' is as follows: Package tutorial.Web; Import java.io.ioException; Import java.io.printwriter; Import javax.naming.context; Import javax.naming.initialcontext; import javax.rmi.portableremoteObject; Import javax.servlet.servletException; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletRequest; Import javax.servlet.http.httpservletResponse; Import tutorial.Interfaces.fibo; Import tutorial.interfaces.fiboHome; / ** * @Author John Doe * * @ 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 { PRIVATE FIBOHOME HOME; PRIVATE STRING VALUE; Public computeServlet () { Super (); } 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"); } } 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 (); } } } Generate servlet related files: To generate a web configuration document, we must first set some xdoclet settings. It is like EJB, let us define the settings of Web automatically generated. Right-click on the selected 'Properties' -> 'XDoclet Configurations', pick the 'add' right on the left window, fill in the 'Web' and press 'OK', so adding an XDoclet 'web' generating setting . Then select 'Web', pick the 'add doclet' -> 'WebDoclet' in the bottom left window, then press' OK '. In the' WebDoclet 'setting of the left hand, set the' DESTDIR 'Fill in' SRC / Web-inf '. Now xdoclet' webDoclet 'settings will generate web files in the' SRC / Web-INF 'data clip.
");