📄 ejb5.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Creating the Web Client</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="EJB4.html" /> <link rel="Next" href="EJB6.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="EJB4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJB6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp79985"> </a><h2 class="pHeading1">Creating the Web Client</h2><a name="wp79988"> </a><p class="pBody">The Web client is contained in the JSP page <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/converter/web/index.jsp</code>. 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. </p><a name="wp79989"> </a><h3 class="pHeading2">Coding the Web Client</h3><a name="wp79990"> </a><p class="pBody">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 J2EE application client. The parameter of the <code class="cCode">lookup</code> method is the only difference; the motivation for using a different name is discussed in <a href="EJB6.html#wp80564">Mapping the Enterprise Bean References</a>.</p><a name="wp79995"> </a><p class="pBody">The classes needed by the client are declared with a JSP <code class="cCode">page</code> directive (enclosed within the <code class="cCode"><%@ %></code> 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 <code class="cCode"><%! %></code> characters) that contains the initialization method, <code class="cCode">jspInit</code>, 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 <code class="cCode"><% %></code> characters) retrieves a parameter from the request and converts it to a <code class="cCode">BigDecimal</code> object. Finally, JSP expressions (enclosed within <code class="cCode"><%= %></code> characters) invoke the enterprise bean's business methods and insert the result into the stream of data returned to the client.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ 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><a name="wp79997"> </a></pre></div><a name="wp79998"> </a><h3 class="pHeading2">Compiling the Web Client</h3><a name="wp80000"> </a><p class="pBody">The J2EE server automatically compiles Web clients that are JSP pages. If the Web client were a servlet, you would have to compile it.</p><a name="wp80002"> </a><h3 class="pHeading2">Packaging the Web Client</h3><a name="wp80004"> </a><p class="pBody">To package a Web client, you run the New Web Component wizard of the <code class="cCode">deploytool</code> utility. During this process the wizard performs the following tasks.</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80005"> </a><div class="pSmartList1"><li>Creates the Web application deployment descriptor</li></div><a name="wp80007"> </a><div class="pSmartList1"><li>Adds the component files to a WAR file</li></div><a name="wp80008"> </a><div class="pSmartList1"><li>Adds the WAR file to the application's <code class="cCode">ConverterApp.ear</code> file</li></div></ul></div><a name="wp80011"> </a><p class="pBody">To start the New Web Component wizard, select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Web Component. The wizard displays the following dialog boxes.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80012"> </a><div class="pSmartList1"><li>Introduction dialog box </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp80013"> </a><div class="pSmartList2"><li>Read the explanatory text for an overview of the wizard's features. </li></div><a name="wp80014"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp80015"> </a><div class="pSmartList1"><li>WAR File dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp80016"> </a><div class="pSmartList2"><li>Select the button labelled Create New WAR Module in Application.</li></div><a name="wp80017"> </a><div class="pSmartList2"><li>In the combo box below this button, select <code class="cCode">ConverterApp</code>.</li></div><a name="wp80018"> </a><div class="pSmartList2"><li>In the WAR Display Name field, enter <code class="cCode">ConverterWAR</code>.</li></div><a name="wp80019"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp80827"> </a><div class="pSmartList2"><li>In the tree under Available Files, locate this directory:</li></div><a name="wp81547"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/converter/web/</code> </p><a name="wp80828"> </a><div class="pSmartList2"><li>Select <code class="cCode">index.jsp</code>.</li></div><a name="wp81167"> </a><div class="pSmartList2"><li>Click Add. </li></div><a name="wp80022"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp80023"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp80024"> </a><div class="pSmartList1"><li>Choose Component Type dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp80025"> </a><div class="pSmartList2"><li>Select the JSP button.</li></div><a name="wp80026"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp80027"> </a><div class="pSmartList1"><li>Component General Properties dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp80028"> </a><div class="pSmartList2"><li>In the JSP Filename combo box, select <code class="cCode">index.jsp</code>.</li></div><a name="wp80029"> </a><div class="pSmartList2"><li>Click Finish.</li></div></ol></div></ol></div><a name="wp80031"> </a><h3 class="pHeading2">Specifying the Web Client's Enterprise Bean Reference</h3><a name="wp80032"> </a><p class="pBody">When it invokes the <code class="cCode">lookup</code> method, the Web client refers to the home of an enterprise bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Object objRef = ic.lookup("java:comp/env/ejb/TheConverter");<a name="wp80033"> </a></pre></div><a name="wp80034"> </a><p class="pBody">You specify this reference as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81175"> </a><div class="pSmartList1"><li>In the tree, select <code class="cCode">ConverterClient</code>.</li></div><a name="wp81176"> </a><div class="pSmartList1"><li>Select the EJB Refs tab.</li></div><a name="wp81177"> </a><div class="pSmartList1"><li>Click Add.</li></div><a name="wp81178"> </a><div class="pSmartList1"><li>In the Coded Name field, enter <code class="cCode">ejb/TheConverter</code>.</li></div><a name="wp81179"> </a><div class="pSmartList1"><li>In the EJB Type field, select Session.</li></div><a name="wp81180"> </a><div class="pSmartList1"><li>In the Interfaces field, select Remote.</li></div><a name="wp81181"> </a><div class="pSmartList1"><li>In the Home Interface column, field converter.<code class="cCode">ConverterHome</code>.</li></div><a name="wp81182"> </a><div class="pSmartList1"><li>In the Local/Remote Interface field, enter <code class="cCode">converter.Converter</code>.</li></div><a name="wp81183"> </a><div class="pSmartList1"><li>In the Enterprise Bean Name field, enter <code class="cCode">ConverterEJB</code>.</li></div><a name="wp81184"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="EJB4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJB6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -