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

📄 ejb4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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 J2EE Application 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="EJB3.html" />    <link rel="Next" href="EJB5.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="EJB3.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="EJB5.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="wp79891"> </a><h2 class="pHeading1">Creating the J2EE Application Client</h2><a name="wp79893"> </a><p class="pBody">A J2EE application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine than the J2EE server. For detailed information on the <code class="cCode">appclient</code> command line tool see the man page at <code class="cCode"><a  href="http://java.sun.com/j2ee/1.4/docs/relnotes/cliref/index.html" target="_blank">http://java.sun.com/j2ee/1.4/docs/relnotes/cliref/index.html</a></code>.</p><a name="wp79895"> </a><p class="pBody">The J2EE application client in this example requires two different JAR files. The first JAR file is for the J2EE component of the client. This JAR file contains the client's deployment descriptor and class files; it is created when you run the New Application Client wizard. Defined by the J2EE Specification, this JAR file is portable across all compliant J2EE servers.</p><a name="wp79896"> </a><p class="pBody">The second JAR file contains stub classes that are required by the client program at runtime. These stub classes enable the client to access the enterprise beans that are running in the J2EE server. The JAR file for the stubs is created by deploytool when you deploy the application. Because this JAR file is not covered by the <span style="font-style: italic">J2EE Specification</span>, it is implementation specific, intended only for the J2EE Application Server. </p><a name="wp79898"> </a><p class="pBody">The J2EE application client source code is in the <code class="cCode">ConverterClient.java</code> file, which is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL</code>&gt;/j2eetutorial14/examples/ejb/converter/src/<a name="wp81529"> </a></pre></div><a name="wp81530"> </a><p class="pBody">You already compiled this code along with the enterprise bean code in the section <a  href="EJB3.html#wp79846">Compiling the Source Files</a>.</p><a name="wp79902"> </a><h3 class="pHeading2">Coding the J2EE Application Client</h3><a name="wp79903"> </a><p class="pBody">The <code class="cCode">ConverterClient.java</code> source code illustrates the basic tasks performed by the client of an enterprise bean:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79904"> </a><div class="pSmartList1"><li>Locating the home interface</li></div><a name="wp79905"> </a><div class="pSmartList1"><li>Creating an enterprise bean instance</li></div><a name="wp79906"> </a><div class="pSmartList1"><li>Invoking a business method</li></div></ul></div><a name="wp79909"> </a><h4 class="pHeading3">Locating the Home Interface</h4><a name="wp79910"> </a><p class="pBody">The <code class="cCode">ConverterHome</code> interface defines life-cycle methods such as <code class="cCode">create</code>. Before the <code class="cCode">ConverterClient</code> can invoke the <code class="cCode">create</code> method, it must locate and instantiate an object whose type is <code class="cCode">ConverterHome</code>. This is a four-step process.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79911"> </a><div class="pSmartList1"><li>Create an initial naming context.</li></div><a name="wp79912"> </a><p class="pBodyRelative"><code class="cCode">Context initial = new InitialContext();</code> </p><a name="wp79915"> </a><p class="pBodyRelative">The <code class="cCode">Context</code> interface is part of the Java Naming and Directory Interface (JNDI). A <span style="font-style: italic">naming contex</span>t is a set of name-to-object bindings. A name that is bound within a context is the <span style="font-style: italic">JNDI name</span> of the object.</p><a name="wp79916"> </a><p class="pBodyRelative">An <code class="cCode">InitialContext</code> object, which implements the <code class="cCode">Context</code> interface, provides the starting point for the resolution of names. All naming operations are relative to a context.</p><a name="wp79919"> </a><div class="pSmartList1"><li>Obtain the environment naming context of the application client.</li></div><a name="wp79920"> </a><p class="pBodyRelative"><code class="cCode">Context myEnv = (Context)initial.lookup(&quot;java:comp/env&quot;);</code> </p><a name="wp79921"> </a><p class="pBodyRelative">The <code class="cCode">java:comp/env</code> name is bound to the environment naming context of the <code class="cCode">ConverterClient</code> component.</p><a name="wp79922"> </a><div class="pSmartList1"><li>Retrieve the object bound to the name <code class="cCode">ejb/SimpleConverter</code>.</li></div><a name="wp79923"> </a><p class="pBodyRelative"><code class="cCode">Object objref = myEnv.lookup(&quot;ejb/SimpleConverter&quot;);</code> </p><a name="wp79925"> </a><p class="pBodyRelative">The <code class="cCode">ejb/SimpleConverter</code> name is bound to an <span style="font-style: italic">enterprise bean reference</span>, a logical name for the home of an enterprise bean. In this case, the <code class="cCode">ejb/SimpleConverter</code> name refers to the <code class="cCode">ConverterHome</code> object. The names of enterprise beans should reside in the <code class="cCode">java:com/env/ejb</code> subcontext.</p><a name="wp79926"> </a><div class="pSmartList1"><li>Narrow the reference to a <code class="cCode">ConverterHome</code> object.</li></div><a name="wp79927"> </a><p class="pBodyRelative"><code class="cCode">ConverterHome home =<br />&nbsp;&nbsp;(ConverterHome) PortableRemoteObject.narrow(objref,<br />&nbsp;&nbsp;ConverterHome.class);</code> </p></ol></div><a name="wp79928"> </a><h4 class="pHeading3">Creating an Enterprise Bean Instance</h4><a name="wp79930"> </a><p class="pBody">To create the bean instance, the client invokes the <code class="cCode">create</code> method on the <code class="cCode">ConverterHome</code> object. The <code class="cCode">create</code> method returns an object whose type is <code class="cCode">Converter</code>. The remote <code class="cCode">Converter</code> interface defines the business methods of the bean that the client may call. When the client invokes the <code class="cCode">create</code> method, the EJB container instantiates the bean and then invokes the <code class="cCode">ConverterBean.ejbCreate</code> method. The client invokes the <code class="cCode">create</code> method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Converter currencyConverter = home.create();<a name="wp79931"> </a></pre></div><a name="wp79932"> </a><h4 class="pHeading3">Invoking a Business Method</h4><a name="wp79934"> </a><p class="pBody">Calling a business method is easy--you simply invoke the method on the <code class="cCode">Converter</code> object. The EJB container will invoke the corresponding method on the <code class="cCode">ConverterEJB</code> instance that is running on the server. The client invokes the <code class="cCode">dollarToYen</code> business method in the following lines of code.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">BigDecimal param = new BigDecimal (&quot;100.00&quot;);BigDecimal amount = currencyConverter.dollarToYen(param);<a name="wp79935"> </a></pre></div><a name="wp79936"> </a><h4 class="pHeading3">ConverterClient Source Code</h4><a name="wp79938"> </a><p class="pBody">The full source code for the <code class="cCode">ConverterClient</code> program follows. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;import java.math.BigDecimal;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -