📄 session2.html
字号:
IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: "+ id); } contents = new Vector(); }<a name="wp79736"> </a></pre></div></ol></div><a name="wp79737"> </a><p class="pBody">Typically, an <code class="cCode">ejbCreate</code> method initializes the state of the enterprise bean. The preceding <code class="cCode">ejbCreate</code> method, for example, initializes the <code class="cCode">customerName</code> and <code class="cCode">customerId</code> variables with the arguments passed by the <code class="cCode">create</code> method. </p><a name="wp79738"> </a><p class="pBody">An enterprise bean must have one or more <code class="cCode">ejbCreate</code> methods. The signatures of the methods must meet the following requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79740"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79741"> </a><div class="pSmartList1"><li>The return type must be <code class="cCode">void</code>.</li></div><a name="wp79742"> </a><div class="pSmartList1"><li>If the bean allows remote access, the arguments must be legal types for the Java Remote Method Invocation ("Java RMI") API.</li></div><a name="wp79743"> </a><div class="pSmartList1"><li>The modifier cannot be <code class="cCode">static</code> or <code class="cCode">final</code>.</li></div></ul></div><a name="wp79744"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include the <code class="cCode">javax.ejb.CreateException</code> and other exceptions that are specific to your application. The <code class="cCode">ejbCreate</code> method usually throws a <code class="cCode">CreateException</code> if an input parameter is invalid.</p><a name="wp79746"> </a><h4 class="pHeading3">Business Methods</h4><a name="wp79747"> </a><p class="pBody">The primary purpose of a session bean is to run business tasks for the client. The client invokes business methods on the remote object reference that is returned by the <code class="cCode">create</code> method. From the client's perspective, the business methods appear to run locally, but they actually run remotely in the session bean. The following code snippet shows how the <code class="cCode">CartClient</code> program invokes the business methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Cart shoppingCart = home.create("Duke DeEarl", "123");...shoppingCart.addBook("The Martian Chronicles"); shoppingCart.removeBook("Alice In Wonderland");bookList = shoppingCart.getContents();<a name="wp79749"> </a></pre></div><a name="wp79750"> </a><p class="pBody">The <code class="cCode">CartBean</code> class implements the business methods in the following code:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void addBook(String title) { contents.addElement(title);}public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + "not in cart."); }}public Vector getContents() { return contents;}<a name="wp79751"> </a></pre></div><a name="wp79753"> </a><p class="pBody">The signature of a business method must conform to these rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79754"> </a><div class="pSmartList1"><li>The method name must not conflict with one defined by the EJB architecture. For example, you cannot call a business method <code class="cCode">ejbCreate</code> or <code class="cCode">ejbActivate</code>. </li></div><a name="wp79755"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79756"> </a><div class="pSmartList1"><li>If the bean allows remote access, the arguments and return types must be legal types for the Java RMI API.</li></div><a name="wp79757"> </a><div class="pSmartList1"><li>The modifier must not be <code class="cCode">static</code> or <code class="cCode">final</code>.</li></div></ul></div><a name="wp79758"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include exceptions that you define for your application. The <code class="cCode">removeBook</code> method, for example, throws the <code class="cCode">BookException</code> if the book is not in the cart.</p><a name="wp79760"> </a><p class="pBody">To indicate a system-level problem, such as the inability to connect to a database, a business method should throw the <code class="cCode">javax.ejb.EJBException</code>. When a business method throws an <code class="cCode">EJBException</code>, the container wraps it in a <code class="cCode">RemoteException</code>, which is caught by the client. The container will not wrap application exceptions such as <code class="cCode">BookException</code>. Because <code class="cCode">EJBException</code> is a subclass of <code class="cCode">RuntimeException</code>, you do not need to include it in the <code class="cCode">throws</code> clause of the business method.</p><a name="wp79764"> </a><h3 class="pHeading2">Home Interface</h3><a name="wp79766"> </a><p class="pBody">A home interface extends the <code class="cCode">javax.ejb.EJBHome</code> interface. For a session bean, the purpose of the home interface is to define the <code class="cCode">create</code> methods that a remote client may invoke. The <code class="cCode">CartClient</code> program, for example, invokes this <code class="cCode">create</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Cart shoppingCart = home.create("Duke DeEarl", "123");<a name="wp79767"> </a></pre></div><a name="wp79770"> </a><p class="pBody">Every <code class="cCode">create</code> method in the home interface corresponds to an <code class="cCode">ejbCreate</code> method in the bean class. The signatures of the <code class="cCode">ejbCreate</code> methods in the <code class="cCode">CartBean</code> class follow:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void ejbCreate(String person) throws CreateException ... public void ejbCreate(String person, String id) throws CreateException <a name="wp79771"> </a></pre></div><a name="wp79772"> </a><p class="pBody">Compare the <code class="cCode">ejbCreate</code> signatures with those of the <code class="cCode">create</code> methods in the <code class="cCode">CartHome</code> interface:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.io.Serializable;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBHome;public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; Cart create(String person, String id) throws RemoteException, CreateException; }<a name="wp79774"> </a></pre></div><a name="wp79775"> </a><p class="pBody">The signatures of the <code class="cCode">ejbCreate</code> and <code class="cCode">create</code> methods are similar, but differ in important ways. The rules for defining the signatures of the <code class="cCode">create</code> methods of a home interface follow.</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79777"> </a><div class="pSmartList1"><li>The number and types of arguments in a <code class="cCode">create</code> method must match those of its corresponding <code class="cCode">ejbCreate</code> method.</li></div><a name="wp79778"> </a><div class="pSmartList1"><li>The arguments and return type of the <code class="cCode">create</code> method must be valid RMI types.</li></div><a name="wp79779"> </a><div class="pSmartList1"><li>A <code class="cCode">create</code> method returns the remote interface type of the enterprise bean. (But an <code class="cCode">ejbCreate</code> method returns <code class="cCode">void</code>.)</li></div><a name="wp79781"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause of the <code class="cCode">create</code> method must include the j<code class="cCode">ava.rmi.RemoteException</code> and the <code class="cCode">javax.ejb.CreateException</code>.</li></div></ul></div><a name="wp79782"> </a><h3 class="pHeading2">Remote Interface</h3><a name="wp79784"> </a><p class="pBody">The remote interface, which extends <code class="cCode">javax.ejb.EJBObject</code>, defines the business methods that a remote client may invoke. Here is the source code for the <code class="cCode">Cart</code> remote interface:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.util.*;import javax.ejb.EJBObject;import java.rmi.RemoteException;public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException;}<a name="wp79786"> </a></pre></div><a name="wp79788"> </a><p class="pBody">The method definitions in a remote interface must follow these rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79789"> </a><div class="pSmartList1"><li>Each method in the remote interface must match a method implemented in the enterprise bean class.</li></div><a name="wp79790"> </a><div class="pSmartList1"><li>The signatures of the methods in the remote interface must be identical to the signatures of the corresponding methods in the enterprise bean class.</li></div><a name="wp79791"> </a><div class="pSmartList1"><li>The arguments and return values must be valid RMI types.</li></div><a name="wp79792"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause must include the <code class="cCode">java.rmi.RemoteException</code>.</li></div></ul></div><a name="wp79794"> </a><h3 class="pHeading2">Helper Classes</h3><a name="wp79796"> </a><p class="pBody">The <code class="cCode">CartEJB</code> session bean has two helper classes: <code class="cCode">BookException</code> and <code class="cCode">IdVerifier</code>. The <code class="cCode">BookException</code> is thrown by the <code class="cCode">removeBook</code> method and the <code class="cCode">IdVerifier</code> validates the <code class="cCode">customerId</code> in one of the <code class="cCode">ejbCreate</code> methods. Helper classes must reside in the EJB JAR file that contains the enterprise bean class.</p><a name="wp79797"> </a><h3 class="pHeading2">Building the CartEJB Example</h3><a name="wp80437"> </a><p class="pBody">Now you are ready to compile the remote interface (<code class="cCode">Cart.java</code>), home interface (<code class="cCode">CartHome.java</code>), the enterprise bean class (<code class="cCode">CartBean.java</code>), the client class (<code class="cCode">CartClient.java</code>), and helper classes (<code class="cCode">BookException.java</code> and <code class="cCode">IdVerifier.java</code>).</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80464"> </a><div class="pSmartList1"><li>In a terminal window, go to this directory:</li></div><a name="wp80465"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/cart/</code> </p><a name="wp80476"> </a><div class="pSmartList1"><li>Type the following command:</li></div><a name="wp80480"> </a><p class="pBodyRelative"><code class="cCode">asant build</code></p></ol></div><a name="wp80489"> </a><h3 class="pHeading2">Creating the Application</h3><a name="wp81249"> </a><p class="pBody">In this section, you'll create a J2EE application named <code class="cCode">CartApp</code>, storing it in the file <code class="cCode">CartApp.ear</code>. </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81250"> </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="wp81251"> </a><div class="pSmartList1"><li>Click Browse.</li></div><a name="wp81252"> </a><div class="pSmartList1"><li>In the file chooser, navigate to <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/ejb/cart/</code>. </li></div><a name="wp81253"> </a><div class="pSmartList1"><li>In the File Name field, enter <code class="cCode">CartApp</code>.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -