📄 session3.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>A Web Service Example: HelloServiceEJB</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="Session2.html" /> <link rel="Next" href="Session4.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="Session2.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="Session4.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="wp79822"> </a><h2 class="pHeading1">A Web Service Example: HelloServiceEJB</h2><a name="wp80083"> </a><p class="pBody"><code class="cCode">HelloServiceEJB</code> is a stateless session bean that implements a single method, <code class="cCode">sayHello</code>. This method matches the <code class="cCode">sayHello</code> method invoked by the clients described in <a href="JAXRPC5.html#wp79960">Creating Web Service Clients with JAX-RPC</a>. Later in this section, you'll test the <code class="cCode">HelloServiceEJB</code> by running one of these JAX-RPC clients.</p><a name="wp80089"> </a><h3 class="pHeading2">Web Service Endpoint Interface</h3><a name="wp80090"> </a><p class="pBody"><code class="cCode">HelloService</code> is the bean's Web service endpoint interface. It provides the client's view of the Web service, hiding the stateless session bean from the client. A Web service endpoint interface must conform to the rules of a JAX-RPC service definition interface. For a summary of these rules, see <a href="JAXRPC4.html#wp80033">Coding the Service Endpoint Interface and Implementation Class</a>. Here is the source code for the <code class="cCode">HelloService</code> interface:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package helloservice;import java.rmi.RemoteException;import java.rmi.Remote;public interface HelloService extends Remote { public String sayHello(String name) throws RemoteException;}<a name="wp80094"> </a></pre></div><a name="wp80095"> </a><h3 class="pHeading2">Stateless Session Bean Implementation Class</h3><a name="wp80096"> </a><p class="pBody">The <code class="cCode">HelloServiceBean</code> class implements the <code class="cCode">sayHello</code> method defined by the <code class="cCode">HelloService</code> interface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces to <code class="cCode">HelloServiceEJB</code>, the methods of the <code class="cCode">HelloServiceBean</code> class could also be accessed by remote clients. No changes to the <code class="cCode">HelloServiceBean</code> class would be necessary. The source code for the <code class="cCode">HelloServiceBean</code> class follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package helloservice;import java.rmi.RemoteException; import javax.ejb.SessionBean;import javax.ejb.SessionContext;public class HelloServiceBean implements SessionBean { public String sayHello(String name) { return "Hello " + name + "from HelloServiceEJB"; } public HelloServiceBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {}} <a name="wp80097"> </a></pre></div><a name="wp80986"> </a><h3 class="pHeading2">Building HelloServiceEJB</h3><a name="wp80987"> </a><p class="pBody">In a terminal window, go to the<code class="cCode"> </code><code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/ejb/helloservice/</code> directory. To build <code class="cCode">HelloServiceEJB</code>, type the following command:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build-service<a name="wp80988"> </a></pre></div><a name="wp80989"> </a><p class="pBody">This command performs the following tasks:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80990"> </a><div class="pSmartList1"><li>Compiles the bean's source code files</li></div><a name="wp80991"> </a><div class="pSmartList1"><li>Creates the <code class="cCode">MyHelloService.wsdl</code> file by running the following <code class="cCode">wscompile</code> command:</li></div><a name="wp80992"> </a><p class="pBodyRelative"><code class="cCode">wscompile -define -d build/output -nd build -classpath build -mapping build/mapping.xml config-interface.xml</code> </p></ul></div><a name="wp80993"> </a><p class="pBody">The <code class="cCode">wscompile</code> tool writes the <code class="cCode">MyHelloService.wsdl</code> file to the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/ejb/helloservice/build/</code> subdirectory. For more information about the <code class="cCode">wscompile</code> tool, see Chapter <a href="JAXRPC.html#wp64024">8</a>.</p><a name="wp80997"> </a><p class="pBody">Use <code class="cCode">deploytool</code> to package and deploy this example.</p><a name="wp80998"> </a><h4 class="pHeading3">Creating the Application</h4><a name="wp80999"> </a><p class="pBody">In this section, you'll create a J2EE application named <code class="cCode">HelloService</code>, storing it in the file <code class="cCode">HelloService.ear</code>. </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81000"> </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="wp81001"> </a><div class="pSmartList1"><li>Click Browse.</li></div><a name="wp81002"> </a><div class="pSmartList1"><li>In the file chooser, navigate to <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/ejb/helloservice/</code>. </li></div><a name="wp81003"> </a><div class="pSmartList1"><li>In the File Name field, enter <code class="cCode">HelloServiceApp</code>.</li></div><a name="wp81004"> </a><div class="pSmartList1"><li>Click New Application.</li></div><a name="wp81005"> </a><div class="pSmartList1"><li>Click OK.</li></div><a name="wp81006"> </a><div class="pSmartList1"><li>Verify that the <code class="cCode">HelloServiceApp.ear</code> file resides in <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/helloservice/</code>.</li></div></ol></div><a name="wp81007"> </a><h4 class="pHeading3">Packaging the Enterprise Bean</h4><a name="wp81008"> </a><p class="pBody">Start the Edit Enterprise Bean wizard by selecting 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>Enterprise JavaBean JAR. The wizard displays the following dialog boxes.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81009"> </a><div class="pSmartList1"><li>Introduction dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81010"> </a><div class="pSmartList2"><li>Read the explanatory text for an overview of the wizard's features.</li></div><a name="wp81011"> </a><div class="pSmartList2"><li>Click Next.</li></div></ol></div><a name="wp81012"> </a><div class="pSmartList1"><li>EJB JAR dialog box</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81013"> </a><div class="pSmartList2"><li>Select the button labelled Create New JAR Module in Application.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -