📄 jaxrpc5.html
字号:
</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Hello Murph!<a name="wp83189"> </a></pre></div><a name="wp117611"> </a><h3 class="pHeading2">J2EE Application Client Example</h3><a name="wp117612"> </a><p class="pBody">Unlike the stand-alone clients in the preceding sections, the client in this section is a J2EE application client. Because it's a J2EE component, a J2EE application client can locate a local Web service by invoking the JNDI <code class="cCode">lookup</code> method.</p><a name="wp117613"> </a><h4 class="pHeading3">J2EE Application HelloClient Listing</h4><a name="wp117614"> </a><p class="pBody">Here is the listing for the <code class="cCode">HelloClient.java</code> file, located in the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/appclient/src/</code> directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package appclient;import javax.xml.rpc.Stub;import javax.naming.*;public class HelloClient { private String endpointAddress; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { Context ic = new InitialContext(); MyHelloService myHelloService = (MyHelloService) ic.lookup("java:comp/env/service/MyJAXRPCHello"); appclient.HelloIF helloPort = myHelloService.getHelloIFPort(); ((Stub)helloPort)._setProperty (Stub.ENDPOINT_ADDRESS_PROPERTY,args[0]); System.out.println(helloPort.sayHello("Jake!")); System.exit(0); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } } }<a name="wp117615"> </a></pre></div><a name="wp117616"> </a><h4 class="pHeading3">Building the J2EE Application Client</h4><a name="wp117617"> </a><p class="pBody">In a terminal window, go to the <code class="cVariable"><INSTALL></code>/<code class="cCode">j2eetutorial14</code>/<code class="cCode">examples/jaxrpc/appclient</code> directory and type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp117618"> </a></pre></div><a name="wp117619"> </a><p class="pBody">The preceding command compiles <code class="cCode">HelloClient.java</code> and runs <code class="cCode">wscompile</code> by invoking the <code class="cCode">generate-mapping</code> target. </p><a name="wp120329"> </a><p class="pBody">This <code class="cCode">asant</code> task also runs <code class="cCode">wscompile</code>, this time to create a <code class="cCode">mapping.xml</code> file and a set of runtime classes. The <code class="cCode">mapping.xml</code> file maps the package names of the service classes to a namespace URI of the WSDL file. The set of runtime classes is made up of serializers, deserializers, and ties. The <code class="cCode">generate-mapping</code> task runs <code class="cCode">wscompile</code> as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">wscompile -gen -d build -nd build -mapping build/mapping.xml -classpath build config-wsdl.xml<a name="wp120330"> </a></pre></div><a name="wp120331"> </a><p class="pBody">The <code class="cCode">-gen</code> flag instructs the tool to read the WSDL file and to create the runtime classes. The <code class="cCode">-d</code> and <code class="cCode">-nd</code> flags tell the tool to write output to the <code class="cCode">build </code>subdirectory, and the <code class="cCode">-mapping</code> flag specifies the mapping file. The tool reads the following <code class="cCode">config-wsdl.xml </code>file:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <wsdl location="../build/MyHelloService.wsdl" packageName="helloservice"/> </configuration><a name="wp120332"> </a></pre></div><a name="wp117624"> </a><h4 class="pHeading3">Packaging the J2EE Application Client</h4><a name="wp117625"> </a><p class="pBody">Packaging this client is a two-step process:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117626"> </a><div class="pSmartList1"><li>Create an EAR file for a J2EE application.</li></div><a name="wp117627"> </a><div class="pSmartList1"><li>Create a JAR file for the application client and add it to the EAR file.</li></div></ol></div><a name="wp117628"> </a><p class="pBody">To create the EAR file, follow these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117629"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, 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>Application EAR.</li></div><a name="wp117630"> </a><div class="pSmartList1"><li>Click Browse.</li></div><a name="wp117631"> </a><div class="pSmartList1"><li>In the file chooser, navigate to <code class="cVariable"><INSTALL></code>/<code class="cCode">j2eetutorial14</code>/<code class="cCode">examples/jaxrpc/appclient</code>. </li></div><a name="wp117632"> </a><div class="pSmartList1"><li>In the File Name field, enter <code class="cCode">HelloServiceApp</code>.</li></div><a name="wp117633"> </a><div class="pSmartList1"><li>Click New Application.</li></div><a name="wp117634"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp117635"> </a><p class="pBody">To start the New Application Client 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>Application Client JAR. The wizard displays the following dialog boxes.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117636"> </a><div class="pSmartList1"><li>Introduction dialog box </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp117637"> </a><div class="pSmartList2"><li>Read the explanatory text for an overview of the wizard's features. </li></div><a name="wp117638"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp117639"> </a><div class="pSmartList1"><li> JAR File Contents dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp117640"> </a><div class="pSmartList2"><li>Select the button labelled Create New AppClient Module in Application.</li></div><a name="wp117641"> </a><div class="pSmartList2"><li>In the combo box below this button, select <code class="cCode">HelloServiceApp</code>.</li></div><a name="wp117642"> </a><div class="pSmartList2"><li>In the AppClient Display Name field, enter <code class="cCode">HelloClient</code>.</li></div><a name="wp117643"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp117644"> </a><div class="pSmartList2"><li>In the tree under Available Files, locate the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/appclient </code>directory.</li></div><a name="wp117645"> </a><div class="pSmartList2"><li>Select the <code class="cCode">build</code> directory.</li></div><a name="wp117646"> </a><div class="pSmartList2"><li>Click Add. </li></div><a name="wp117647"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp117648"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp117649"> </a><div class="pSmartList1"><li>General dialog box </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp117650"> </a><div class="pSmartList2"><li>In the Main Class combo box, select <code class="cCode">appclient.HelloClient</code>.</li></div><a name="wp117651"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp117652"> </a><div class="pSmartList2"><li>Click Finish.</li></div></ol></div></ol></div><a name="wp117654"> </a><h4 class="pHeading3">Specifying the Web Reference</h4><a name="wp117655"> </a><p class="pBody">When it invokes the <code class="cCode">lookup</code> method, the <code class="cCode">HelloClient</code> refers to the Web service as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">MyHelloService myHelloService = (MyHelloService)ic.lookup("java:comp/env/service/MyJAXRPCHello");<a name="wp117656"> </a></pre></div><a name="wp117657"> </a><p class="pBody">You specify this reference as follows.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117658"> </a><div class="pSmartList1"><li>In the tree, select <code class="cCode">HelloClient</code>.</li></div><a name="wp117659"> </a><div class="pSmartList1"><li>Select the Web Service Refs tab.</li></div><a name="wp117660"> </a><div class="pSmartList1"><li>Click Add.</li></div><a name="wp117661"> </a><div class="pSmartList1"><li>In the Coded Name field, enter <code class="cCode">service/MyJAXRPCHello</code>.</li></div><a name="wp117662"> </a><div class="pSmartList1"><li>In the Service Interface combo box, select <code class="cCode">appclient.MyHelloService</code>.</li></div><a name="wp117663"> </a><div class="pSmartList1"><li>In the WSDL File combo box, select <code class="cCode">MyHelloService.wsdl</code>.</li></div><a name="wp117664"> </a><div class="pSmartList1"><li>In the Namespace field, enter <code class="cCode">urn:Foo</code>.</li></div><a name="wp117665"> </a><div class="pSmartList1"><li>In the Local Part field, enter <code class="cCode">MyHelloService</code>.</li></div><a name="wp117666"> </a><div class="pSmartList1"><li>In the Mapping File combo box, select <code class="cCode">mapping.xml.</code></li></div><a name="wp117667"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp117668"> </a><h4 class="pHeading3">Deploying and Running the J2EE Application Client</h4><a name="wp117669"> </a><p class="pBody">Before performing the steps in this section, you must first create and deploy the <code class="cCode">MyHelloService</code> as described in <a href="JAXRPC4.html#wp115211">Creating a Web Service with JAX-RPC</a>.</p><a name="wp117673"> </a><p class="pBody">To deploy the J2EE application client, follow these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117674"> </a><div class="pSmartList1"><li>Select the <code class="cCode">HelloServiceApp</code> application.</li></div><a name="wp117675"> </a><div class="pSmartList1"><li>Select Tools<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Deploy.</li></div><a name="wp117676"> </a><div class="pSmartList1"><li>In the Deploy Module dialog select the checkbox labelled Return Client JAR.</li></div><a name="wp117677"> </a><div class="pSmartList1"><li>In the field below the checkbox, enter this directory:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cVariable"><INSTALL></code>/j2eetutorial14/<code class="cCode">examples/jaxrpc/appclient</code><a name="wp117678"> </a></pre></div><a name="wp117679"> </a><div class="pSmartList1"><li>Click OK.</li></div></ol></div><a name="wp117680"> </a><p class="pBody">To run the client:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp117681"> </a><div class="pSmartList1"><li>In a terminal window, go to the <code class="cVariable"><INSTALL></code>/<code class="cCode">j2eetutorial14</code>/<code class="cCode">examples/jaxrpc/appclient/</code> directory.</li></div><a name="wp117682"> </a><div class="pSmartList1"><li>Type the following on a single line:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">appclient -client HelloServiceAppClient.jarhttp://localhost:8080/hello-jaxrpc/hello<a name="wp117683"> </a></pre></div></ol></div><a name="wp117684"> </a><p class="pBody">The client should display this line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Hello Jake!<a name="wp117685"> </a></pre></div><a name="wp83661"> </a><h3 class="pHeading2">More JAX-RPC Client Examples</h3><a name="wp93018"> </a><p class="pBody">Other chapters in this book also have JAX-RPC client examples:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp117693"> </a><div class="pSmartList1"><li>Chapter <a href="Security.html#wp79660">27</a> includes a static stub client that demonstrates authentication. See the section, <a href="Security7.html#wp156943">Example: Basic Authentication with JAX-RPC</a>.</li></div><a name="wp117700"> </a><div class="pSmartList1"><li>Chapter <a href="JSPAdvanced.html#wp65706">16</a> shows how a JSP page can be a static stub client that accesses a remote Web service. See the section, <a href="JSPAdvanced2.html#wp71778">The Example JSP Pages</a>.</li></div></ul></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="JAXRPC4.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="JAXRPC6.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 + -