Prev | Next

TOC | Index

J2EETM Developer's Guide


Servlet clients allow web browsers to indirectly access enterprise beans. The following diagram illustrates this access:

FIGURE 7-1 Servlet Client of an Enterprise Bean

The sections that follow show how to create a servlet client called AdderServlet. This servlet accesses an enterprise bean implemented by the AdderEJB class. The enterprise bean maintains a running total that is incremented by a value entered by the end-user in a browser. The source code for AdderServlet and AdderEJB reside in the doc/guides/ejb/examples/adder directory.

Setting Up the Servlet's J2EE Application

The J2EE application in this example contains an enterprise bean and a web component. The enterprise bean is packaged in an EJB .jar file and the web component in a .war file. A later section ("Creating the Servlet's .war File") explains how to package the AdderServlet.class and adder.html files. But first, you should prepare the J2EE application:

Coding the Servlet

This section briefly describes the AdderServlet example. If you need additional information about servlets, please visit the JavaTM Servlet API web page .

Immediately after it creates the AdderServlet instance, the web server calls intance's init method. This method looks up the enterprise bean, whose coded name is ejb/Adder, and then creates the bean:

public void init() throws ServletException {

    try {
        InitialContext ic = new InitialContext();
        Object objref = ic.lookup("java:comp/env/ejb/Adder");
        AdderHome home = 
        adder = home.create(0);
    } catch(Exception e) {

When the end-user clicks the Submit Query button on the HTML form, the web server calls the doGet method of the AdderServlet. The doGet method fetches the inputString entered by the end-user, converts it an integer, and adds it to the running total in the enterprise bean by invoking the add business method. To retrieve the running total, the doGet method calls the getTotal business method. The source code for the doGet method follows:

public void doGet (HttpServletRequest req, HttpServletResponse res)
                   throws ServletException, IOException {

   String inputString = req.getParameter("inputString");
   Integer inputNumber = new Integer(inputString);
   int total = adder.getTotal();
   PrintWriter out = res.getWriter();

The generatePage method, called by doGet, formats the HTML page that displays the running total:

private void generatePage(PrintWriter out, int total) {

   out.println("<title>Input for AdderServlet</title>");
   out.println("The running total is: " + String.valueOf(total));
   out.println("<form method = get action=\"AdderAlias\">");
   out.println("Please enter an integer:");
   out.println("<input type=text name=\"inputString\">");
   out.println("<input type=submit>");

Compiling the Servlet

When compiling, you must include the j2ee.jar file in the classpath:


javac -classpath "$CPATH" 

set CPATH=.;%J2EE_HOME%\lib\j2ee.jar
javac -classpath %CPATH%

Coding the HTML File

The adder.htmll file displays a form that prompts the user for input. When the user clicks the form's Submit button, the server invokes the AdderServlet class, which you will map to the AdderAlias in the next section. Here is the content of the adder.html file:

<title>Initial Page for AdderServlet</title>
<form method = get action="AdderAlias">
Please enter an integer:
<input type=text name="inputString">
<input type=submit>

Creating the Servlet's .war File

To package the AdderServlet.class and adder.html files in a .war file, you run the New Web Component Wizard of the Application Deployment Tool. To start the wizard, from the File menu choose New Web Component. The wizard displays the following dialog boxes. (You may skip any dialog boxes not listed here.)

WAR File General Properties Dialog Box:

a. In the combo box labelled "Web Component Will Go In," select AdderApp.
b. In the WAR Display Name field, enter AdderWAR.
c. Click Add.
d. In the Add Content Files dialog box, choose the root directory containing the adder.html file (examples/adder). You may either type the directory name in the Root Directory field or locate it by clicking Browse.
e. Select the adder.html file from the text area and click Add.
f. Click Next.
g. In the Add Class Files dialog box choose, choose the examples/adder directory again.
h. Select the AdderServlet.class file from the text area and click Add.
i. Click Finish.
j. Click Next.
Choose Component Type Dialog Box:

a. Select Servlet.
b. Click Next.
Component General Properties Dialog Box:

a. In the Servlet Class combo box, select AdderServlet.
b. In the Web Component Display Name field, enter TheAdder.
c. Click Next.
Component Aliases Dialog Box:

a. Click Add.
b. In the Aliases list, enter AdderAlias.
c. Click Next.
Enterprise Bean References Dialog Box:

a. Click Add.
b. In the Coded Name column enter ejb/Adder.
c. In the Type column select Session.
d. In the Home column enter AdderHome.
e. In the Remote column enter Adder.
f. Click Finish.

Specifying the Web Context Root

1. In the tree view select AdderApp.

2. In the Web Context tabbed pane, enter AdderContextRoot in the ContextRoot column.

Specifying the JNDI Names

In the JNDI Names tabbed pane for the AdderApp, specify MyAdder as the JNDI name for both the ejb/Adder reference and the AdderBean component.

Deploying the Servlet's J2EE Application

1. From the Tools menu, choose Deploy Application.

2. In the first dialog box, do not select the checkbox labelled "Return Client Jar."

3. In the second dialog box, verify the JNDI names.

4. In the third dialog box, verify the context root.

Running the Servlet

To run the AdderServlet program from your browser, specify the URL as follows, but replace <host> with the name of the machine that is running the J2EE server:

Enter an integer in the field and click the Submit Query button. Repeat this process and note the running total displayed at the top of the page.

Prev | Next

TOC | Index

Copyright © 2000 Sun Microsystems, Inc. All rights reserved.