📄 session2.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>The CartEJB Example</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="Session.html" /> <link rel="Next" href="Session3.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="Session.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="Session3.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="wp79698"> </a><h2 class="pHeading1">The CartEJB Example</h2><a name="wp79699"> </a><p class="pBody">The <code class="cCode">CartEJB</code> session bean represents a shopping cart in an online bookstore. The bean's client may add a book to the cart, remove a book, or retrieve the cart's contents. To construct <code class="cCode">CartEJB</code>, you need the following code:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79700"> </a><div class="pSmartList1"><li>Session bean class (<code class="cCode">CartBean</code>)</li></div><a name="wp79701"> </a><div class="pSmartList1"><li>Home interface (<code class="cCode">CartHome</code>)</li></div><a name="wp79702"> </a><div class="pSmartList1"><li>Remote interface (<code class="cCode">Cart</code>)</li></div></ul></div><a name="wp79703"> </a><p class="pBody">All session beans require a session bean class. All enterprise beans that permit remote access must have a home and remote interface. To meet the needs of a specific application, an enterprise bean may also need some helper classes. The <code class="cCode">CartEJB</code> session bean uses two helper classes, <code class="cCode">BookException</code> and <code class="cCode">IdVerifier</code>, which are discussed in the section <a href="Session2.html#wp79794">Helper Classes</a>.</p><a name="wp79707"> </a><p class="pBody">The source code for this example is in the <code class="cCode"><</code><code class="cVariable">J2EE_HOME</code><code class="cCode">>/j2eetutorial14/examples/ejb/cart/</code> directory. </p><a name="wp79709"> </a><h3 class="pHeading2">Session Bean Class</h3><a name="wp79710"> </a><p class="pBody">The session bean class for this example is called <code class="cCode">CartBean</code>. Like any session bean, the <code class="cCode">CartBean</code> class must meet these requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79712"> </a><div class="pSmartList1"><li>It implements the <code class="cCode">SessionBean</code> interface.</li></div><a name="wp79713"> </a><div class="pSmartList1"><li>The class is defined as <code class="cCode">public</code>.</li></div><a name="wp79714"> </a><div class="pSmartList1"><li>The class cannot be defined as <code class="cCode">abstract</code> or <code class="cCode">final</code>.</li></div><a name="wp79715"> </a><div class="pSmartList1"><li>It implements one or more <code class="cCode">ejbCreate</code> methods.</li></div><a name="wp79716"> </a><div class="pSmartList1"><li>It implements the business methods.</li></div><a name="wp79717"> </a><div class="pSmartList1"><li>It contains a <code class="cCode">public</code> constructor with no parameters.</li></div><a name="wp79718"> </a><div class="pSmartList1"><li>It must not define the <code class="cCode">finalize</code> method.</li></div></ul></div><a name="wp79719"> </a><p class="pBody"> The source code for the <code class="cCode">CartBean</code> class follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.util.*;import javax.ejb.*;public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); } public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new CreateException("Invalid id: "+ id); } contents = new Vector(); } 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; } public CartBean() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {}} <a name="wp79720"> </a></pre></div><a name="wp79721"> </a><h4 class="pHeading3">The SessionBean Interface</h4><a name="wp79726"> </a><p class="pBody">The <code class="cCode"><a href="http://java.sun.com/j2ee/tutorial/api/javax/ejb/SessionBean.html" target="_blank">SessionBean</a></code> interface extends the <code class="cCode"><a href=" http://java.sun.com/j2ee/tutorial/api/javax/ejb/EnterpriseBean.html" target="_blank">EnterpriseBean</a></code> interface, which in turn extends the <code class="cCode">Serializable</code> interface. The <code class="cCode">SessionBean</code> interface declares the <code class="cCode">ejbRemove</code>, <code class="cCode">ejbActivate</code>, <code class="cCode">ejbPassivate</code>, and <code class="cCode">setSessionContext</code> methods. The <code class="cCode">CartBean</code> class doesn't use these methods, but it must implement them because they're declared in the <code class="cCode">SessionBean</code> interface. Consequently, these methods are empty in the <code class="cCode">CartBean</code> class. Later sections explain when you might use these methods.</p><a name="wp79728"> </a><h4 class="pHeading3">The ejbCreate Methods</h4><a name="wp79730"> </a><p class="pBody">Because an enterprise bean runs inside an EJB container, a client cannot directly instantiate the bean. Only the EJB container can instantiate an enterprise bean. During instantiation, the example program performs the following steps.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp79732"> </a><div class="pSmartList1"><li>The client invokes a <code class="cCode">create</code> method on the home object:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> Cart shoppingCart = home.create("Duke DeEarl","123");<a name="wp79733"> </a></pre></div><a name="wp79734"> </a><div class="pSmartList1"><li>The EJB container instantiates the enterprise bean.</li></div><a name="wp79735"> </a><div class="pSmartList1"><li>The EJB container invokes the appropriate <code class="cCode">ejbCreate</code> method in <code class="cCode">CartBean</code>:</li></div><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> public void ejbCreate(String person, String id) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -