Prev | Next | J2EETM Developer's Guide
Clients |
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.
AdderServlet.class
and adder.html
files. But first, you should prepare the J2EE application:
AdderApp
.
class. AdderBean
.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:
When the end-user clicks the Submit Query button on the HTML form, the web server calls thepublic void init() throws ServletException { try { InitialContext ic = new InitialContext(); Object objref = ic.lookup("java:comp/env/ejb/Adder"); AdderHome home = (AdderHome)PortableRemoteObject.narrow(objref, AdderHome.class); adder = home.create(0); } catch(Exception e) { e.printStackTrace(); } }
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:
Thepublic void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String inputString = req.getParameter("inputString"); Integer inputNumber = new Integer(inputString); adder.add(inputNumber.intValue()); int total = adder.getTotal(); res.setContentType("text/html"); PrintWriter out = res.getWriter(); generatePage(out,total); }
generatePage
method, called by doGet
, formats the HTML page that displays the running total:
private void generatePage(PrintWriter out, int total) { out.println("<html>"); out.println("<head>"); out.println("<title>Input for AdderServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("The running total is: " + String.valueOf(total)); out.println("<p>"); out.println("<form method = get action=\"AdderAlias\">"); out.println("Please enter an integer:"); out.println("<input type=text name=\"inputString\">"); out.println("<p>"); out.println("<input type=submit>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); }
AdderServlet.java
, you must include the j2ee.jar
file in the classpath:
Windows:CPATH=.:$J2EE_HOME/lib/j2ee.jar javac -classpath "$CPATH" AdderServlet.java
set CPATH=.;%J2EE_HOME%\lib\j2ee.jar javac -classpath %CPATH% AdderServlet.java
l
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:
<html> <head> <title>Initial Page for AdderServlet</title> </head> <body> <form method = get action="AdderAlias"> Please enter an integer: <input type=text name="inputString"> <p> <input type=submit> </form> </body> </html>
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 theadder.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.
2. In the Web Context tabbed pane, enter AdderContextRoot
in the ContextRoot column.
AdderApp
, specify MyAdder
as the JNDI name for both the ejb/Adder
reference and the AdderBean
component.
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.
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.http://<host>:8000/AdderContextRoot/adder.html