Chapter 6. The Servlet and the Web-App

Having an EJB is not enough. We will write a servlet that access this EJB to perform the actual computation of the Fibonacci suite.

Create a new HTTP Servlet. Select File > New > Other... and choose JBoss-IDE > Web Components > HTTP Servlet.

Procedure 6.1. HTTP Servlet Configuration

  1. Set the Package to tutorial.web.
  2. Set the Class Name to ComputeServlet.
  3. Under Which method stubs would you like to create?, check the init() method.
  4. 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.

protected void doPost(HttpServletRequest request,
	HttpServletResponse response) throws ServletException, IOException {

	response.setContentType("text/html");
	PrintWriter out = response.getWriter();

	out.println("<html><head><title>");
	out.println("Fibonaci Computation");
	out.println("</title></head>");
	out.println("<body>");

	out.println("<h1>");
	out.println("Fibonaci Computation");
	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(limit);
		bean.remove();

		out.println("<p>");
		out.print("The ");
		out.print(limit);
		out.print(" first Fibonacci numbers ");

		for (int i = 0; i < result.length; i++) {
			out.println("<br>");
			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();
	}
}
	

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.

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.ServletConfig;
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;

/**
 * @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 {
	private FiboHome home;

	public ComputeServlet() {
		super();
	}

      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");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.println("<html><head><title>");
		out.println("Fibonaci Computation");
		out.println("</title></head>");
		out.println("<body>");

		out.println("<h1>");
		out.println("Fibonaci Computation");
		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(limit);
			bean.remove();

			out.println("<p>");
			out.print("The ");
			out.print(limit);
			out.print(" first Fibonacci numbers ");

			for (int i = 0; i < result.length; i++) {
				out.println("<br>");
				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();
		}
	}
}