⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ejb5.html

📁 j2eePDF格式的电子书
💻 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">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/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">&lt;%@ %&gt;</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">&lt;%! %&gt;</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">&lt;% %&gt;</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">&lt;%= %&gt;</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">&lt;%@ page import=&quot;Converter,ConverterHome,javax.ejb.*,javax.naming.*, javax.rmi.PortableRemoteObject,java.rmi.RemoteException&quot; %&gt;&lt;%!&nbsp;&nbsp;private Converter converter = null;&nbsp;&nbsp;public void jspInit() {&nbsp;&nbsp;&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InitialContext ic = new InitialContext();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object objRef = ic.lookup(&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;java:comp/env/ejb/TheConverter&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ConverterHome home =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(ConverterHome)PortableRemoteObject.narrow(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRef, ConverterHome.class);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;converter = home.create();&nbsp;&nbsp;&nbsp;&nbsp;} catch (RemoteException ex) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}&nbsp;&nbsp;...%&gt;&lt;html&gt;&lt;head&gt;&nbsp;&nbsp; &lt;title&gt;Converter&lt;/title&gt;&lt;/head&gt;&lt;body bgcolor=&quot;white&quot;&gt;&lt;h1&gt;&lt;center&gt;Converter&lt;/center&gt;&lt;/h1&gt;&lt;hr&gt;&lt;p&gt;Enter an amount to convert:&lt;/p&gt;&lt;form method=&quot;get&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;amount&quot; size=&quot;25&quot;&gt;&lt;br&gt;&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;&lt;input type=&quot;reset&quot; value=&quot;Reset&quot;&gt;&lt;/form&gt;&lt;%&nbsp;&nbsp;String amount = request.getParameter(&quot;amount&quot;);&nbsp;&nbsp;if ( amount != null &amp;&amp; amount.length() &gt; 0 ) {&nbsp;&nbsp;&nbsp;&nbsp;BigDecimal d = new BigDecimal (amount);%&gt;&nbsp;&nbsp;&lt;p&gt;&lt;%= amount %&gt; dollars are  &nbsp;&nbsp;&nbsp;&nbsp;&lt;%= converter.dollarToYen(d) %&gt;  Yen.&nbsp;&nbsp;&lt;p&gt;&lt;%= amount %&gt; Yen are &nbsp;&nbsp;&nbsp;&nbsp;&lt;%= converter.yenToEuro(d) %&gt;  Euro.&lt;%&nbsp;&nbsp; }%&gt;&lt;/body&gt;&lt;/html&gt;<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">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/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(&quot;java:comp/env/ejb/TheConverter&quot;);<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 + -