Having an EJB is not enough. We will write a servlet that access this EJB to
perform the actual computation of the Fibonacci suite.
A D V E R T I S E M E N T
Create a new HTTP Servlet. Select
File > New >
Other... and choose JBoss-IDE >
Web Components > HTTP
Servlet.
Procedure HTTP Servlet Configuration
Set the Package to
tutorial.web.
Set the Class Name to
ComputeServlet.
Under Which method stubs would you
like to create?, check the init()
method.
Under Which service method stubs
would like to create?, check the
doPost() method.
Our servlet needs some initialization and processing code. Add the following
private member.
private FiboHome home;
Complete the init method as shown. This code is
responsible for the initialization of the EJB Home interface and grabbing the
local environment entry.
public void init(ServletConfig config) throws ServletException {
try {
Context context = new InitialContext();
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");
}
}
Complete the doPost method as shown. The code will
parse the request to get the limit parameter, create an instance of the EJB,
perform computation, release the instance and output the result as HTML.
Next, we will insert the missing XDoclet tags for
the Servlet. In the Java editor go in the Javadoc class paragraph.
Type �@web.� And press CTRL+Space. You should see JBoss Eclipse
IDE's auto-completion in action.
Correct and complete the attributes of the tag with the following values
(press CTRL+Space for each attribute if you want the completion) :
/**
* @web.servlet
* name="Compute"
* display-name="Computation Servlet"
* description="Servlet that compute Fibonacci suite"
*
* @web.servlet-mapping
* url-pattern="/Compute"
*
* @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/Fibo"
*/
public class ComputeServlet extends HttpServlet {
After that, the file should look like this. Now we are ready to run XDoclet
on the file, which will generate the Web descriptors.