📄 ejb3.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>Creating the Enterprise Bean</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="EJB2.html" /> <link rel="Next" href="EJB4.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="EJB2.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="EJB4.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="wp79820"> </a><h2 class="pHeading1">Creating the Enterprise Bean</h2><a name="wp79823"> </a><p class="pBody">An <span style="font-style: italic">enterprise bean</span> is a server-side component that contains the business logic of an application. At runtime, the clients execute the business logic by invoking the enterprise bean's methods. The enterprise bean in our example is a stateless session bean called <code class="cCode">ConverterEJB</code>. The source code for <code class="cCode">ConverterEJB</code> is in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/converter/src/</code> directory.</p><a name="wp80603"> </a><p class="pBody">Creating <code class="cCode">ConverterEJB</code> requires these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80604"> </a><div class="pSmartList1"><li>Coding the bean's interfaces and class (the source code is provided)</li></div><a name="wp80605"> </a><div class="pSmartList1"><li>Compiling the source code with <code class="cCode">asant</code></li></div><a name="wp80609"> </a><div class="pSmartList1"><li>With deploytool, packaging the bean into an EJB JAR file and inserting the EJB JAR file into the application's <code class="cCode">ConverterApp.ear</code> file</li></div></ol></div><a name="wp79824"> </a><h3 class="pHeading2">Coding the Enterprise Bean</h3><a name="wp79825"> </a><p class="pBody">The enterprise bean in this example needs the following code:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79826"> </a><div class="pSmartList1"><li>Remote interface </li></div><a name="wp79827"> </a><div class="pSmartList1"><li>Home interface </li></div><a name="wp79828"> </a><div class="pSmartList1"><li>Enterprise bean class </li></div></ul></div><a name="wp79829"> </a><h4 class="pHeading3">Coding the Remote Interface</h4><a name="wp79831"> </a><p class="pBody">A r<span style="font-style: italic">emote interface</span> defines the business methods that a client may call. The business methods are implemented in the enterprise bean code. The source code for the <code class="cCode">Converter</code> remote interface follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.ejb.EJBObject;import java.rmi.RemoteException;import java.math.*;public interface Converter extends EJBObject { public BigDecimal dollarToYen(BigDecimal dollars) throws RemoteException; public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;}<a name="wp79833"> </a></pre></div><a name="wp79834"> </a><h4 class="pHeading3">Coding the Home Interface</h4><a name="wp79835"> </a><p class="pBody">A <span style="font-style: italic">home interface</span> defines the methods that allow a client to create, find, or remove an enterprise bean. The <code class="cCode">ConverterHome</code> interface contains a single create method, which returns an object of the remote interface type. Here is the source code for the <code class="cCode">ConverterHome</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 ConverterHome extends EJBHome { Converter create() throws RemoteException, CreateException;}<a name="wp79837"> </a></pre></div><a name="wp79838"> </a><h4 class="pHeading3">Coding the Enterprise Bean Class</h4><a name="wp79839"> </a><p class="pBody">The enterprise bean class for this example is called <code class="cCode">ConverterBean</code>. This class implements the two business methods, <code class="cCode">dollarToYen</code> and <code class="cCode">yenToEuro</code>, that the <code class="cCode">Converter</code> remote interface defines. The source code for the <code class="cCode">ConverterBean</code> class follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.rmi.RemoteException; import javax.ejb.SessionBean;import javax.ejb.SessionContext;import java.math.*;public class ConverterBean implements SessionBean { BigDecimal yenRate = new BigDecimal("121.6000"); BigDecimal euroRate = new BigDecimal("0.0077"); public BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -