Download
FAQ History |
API
Search Feedback |
Creating the Web Client
The Web client is contained in the JSP page
<
INSTALL
>/j2eetutorial14/examples/ejb/converter/web/index.jsp
. A JSP page is a text-based document that contains JSP elements, which construct dynamic content, and static template data, which can be expressed in any text-based format such as HTML, WML, and XML.Coding the Web Client
The statements (in bold in the following code) for locating the home interface, creating an enterprise bean instance, and invoking a business method are nearly identical to those of the application client. The parameter of the
lookup
method is the only difference; the motivation for using a different name is discussed in Mapping the Enterprise Bean References.The classes needed by the client are declared using a JSP
page
directive (enclosed within the<%@ %>
characters). Because locating the home interface and creating the enterprise bean are performed only once, this code appears in a JSP declaration (enclosed within the<%! %>
characters) that contains the initialization method,jspInit
, of the JSP page. The declaration is followed by standard HTML markup for creating a form that contains an input field. A scriptlet (enclosed within the<% %>
characters) retrieves a parameter from the request and converts it to aBigDecimal
object. Finally, JSP expressions (enclosed within<%= %>
characters) invoke the enterprise bean's business methods and insert the result into the stream of data returned to the client.<%@ page import="Converter,ConverterHome,javax.ejb.*, javax.naming.*, javax.rmi.PortableRemoteObject, java.rmi.RemoteException" %> <%! private Converter converter = null; public void jspInit() { try {InitialContext ic = new InitialContext(); Object objRef = ic.lookup(" java:comp/env/ejb/TheConverter"); ConverterHome home = (ConverterHome)PortableRemoteObject.narrow( objRef, ConverterHome.class); converter = home.create();
} catch (RemoteException ex) { ... } } ... %> <html> <head> <title>Converter</title> </head> <body bgcolor="white"> <h1><center>Converter</center></h1> <hr> <p>Enter an amount to convert:</p> <form method="get"> <input type="text" name="amount" size="25"> <br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> <% String amount = request.getParameter("amount"); if ( amount != null && amount.length() > 0 ) { BigDecimal d = new BigDecimal (amount); %> <p><%= amount %> dollars are<%= converter.dollarToYen(d) %>
Yen. <p><%= amount %> Yen are<%= converter.yenToEuro(d) %>
Euro. <% } %> </body> </html>Compiling the Web Client
The Application Server automatically compiles Web clients that are JSP pages. If the Web client were a servlet, you would have to compile it.
Packaging the Web Client
To package a Web client, you run the New Web Component wizard of the
deploytool
utility. During this process the wizard performs the following tasks.To start the New Web Component wizard, select FileNewWeb Component. The wizard displays the following dialog boxes.
- Introduction dialog box
- WAR File dialog box
- Select the button labeled Create New WAR Module in Application.
- In the combo box below this button, select
ConverterApp
.- In the WAR Name field, enter
ConverterWAR
.- Click Edit Contents.
- In the tree under Available Files, locate this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/converter/web/
- Select
index.jsp
.- Click Add.
- Click OK.
- Click Next.
- Choose Component Type dialog box
- Component General Properties dialog box
Specifying the Web Client's Enterprise Bean Reference
When it invokes the
lookup
method, the Web client refers to the home of an enterprise bean:You specify this reference as follows:
- In the tree, select
ConverterWAR
.- Select the EJB Ref's tab.
- Click Add.
- In the Coded Name field, enter
ejb/TheConverter
.- In the EJB Type field, select Session.
- In the Interfaces field, select Remote.
- In the Home Interface field, enter
converter.ConverterHome
.- In the Local/Remote Interface field, enter
converter.Converter
.- In the JNDI Name field, select
ConverterBean
.- Click OK.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.