Home TOC |
Search
Feedback |
Creating the Web Client
The web client is contained in the JSP page examples/src/ejb/converter/index.jsp. A JSP page is a text-based document that contains static template data, which can be expressed in any text-based format such as HTML, WML, and XML and JSP elements, which construct dynamic content.
Coding the Web Client
The statements (highlighted below) for locating the home interface, creating an enterprise bean instance, and invoking a business method are nearly identical to those of the J2EE application client. (The parameter of the lookup method is the only difference.) Because the first two functions are performed only once, they appear 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 with an input field. A scriptlet (enclosed within the <% %> characters) retrieves a parameter from the request and converts it to a double. 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="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 ) { Double d = new Double (amount); %> <p><%= amount %> dollars are <%= converter.dollarToYen(d.doubleValue()) %> Yen. <p><%= amount %> Yen are <%= converter.yenToEuro(d.doubleValue()) %> Euro. <% } %> </body> </html>Compiling the Web Client
The J2EE server automatically compiles the web client.
Packaging the Web Client
To package a web component, you run the New Web Component Wizard of the deploytool. During this process, the wizard puts the client files into a WAR file and then adds the WAR file to the application's ConverterApp.ear file.
To start the New Web Component Wizard, select File->New Web Component. The wizard displays the following dialog boxes.
- Introduction Dialog Box:
- WAR File General Properties Dialog Box
- In the combo box labelled Create New WAR File in Application, select ConverterApp.
- In the WAR Display Name field, enter ConverterWAR.
- Click Add.
- Navigate to examples/build/ejb/converter.
- 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 an enterprise bean:
Object objref = initial.lookup ("java:comp/env/ejb/TheConverter");
Home TOC |
Search
Feedback |