📄 ch02.html
字号:
<LI CLASS="BL"><A NAME="pgfId-1087512"></A><EM CLASS="CODE">BidderBean</EM><A NAME="marker-1087511"></A> is a session bean that uses <EM CLASS="CODE">AuctionItemBean</EM> to retrieve a list of all auction items, only new items, items due to close, and items whose summary matches a search string from the database. It also checks the user ID and password when someone places a bid, and it stores new bids in the database. </LI><LI CLASS="BL"><A NAME="pgfId-1087514"></A><EM CLASS="CODE">SellerBean</EM><A NAME="marker-1087513"></A> is a session bean that uses <EM CLASS="CODE">RegistrationBean</EM> to check the user ID and password when someone posts an auction item, and it uses <EM CLASS="CODE">AuctionItemBean</EM> to add new auction items to the database.</LI></UL><P CLASS="Body"><A NAME="pgfId-1087516"></A><A NAME="marker-1087515"></A>An entity or session bean is really a collection of interfaces and classes. All entity and session beans consist of a remote interface, home interface, and the bean class. The servlet looks up the beans's home interface running in the Enterprise JavaBeans server, uses the home interface to create the remote interface, and invokes bean methods through the remote interface. An enterprise bean's interfaces and container do the following:</P><UL><LI CLASS="BL"><A NAME="pgfId-1087518"></A><A NAME="marker-1087517"></A>An enterprise bean's remote interface describes the bean's methods, or what the bean does. A client program or another enterprise bean calls the methods defined in the remote interface to invoke the business logic implemented by the bean.</LI><LI CLASS="BL"><A NAME="pgfId-1087520"></A><A NAME="marker-1087519"></A>An enterprise bean's home interface describes how a client program or another enterprise bean creates, finds (entity beans only), and removes that enterprise bean from its container. </LI><LI CLASS="BL"><A NAME="pgfId-1087522"></A><A NAME="marker-1087521"></A>The container provides the interface between the enterprise bean and the low-level implementations in a given Enterprise JavaBeans server.</LI></UL></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087523"></A>Developing and Running Applications</H5><P CLASS="Body"><A NAME="pgfId-1087524"></A>Deployment tools and an Enterprise JavaBeans server are essential to running Enterprise JavaBeans applications. Deployment tools generate <A NAME="marker-1087525"></A><A NAME="marker-1087526"></A>containers. The server provider can include containers and deployment tools for their own server and will typically publish their low-level interfaces so other vendors can develop containers and deployment tools for their server. The auction house example uses the Enterprise JavaBeans server and deployment tools created by <EM CLASS="A">BEA Weblogic (</EM><EM CLASS="URL-Footnote">http://www.beasys.com/weblogic.html</EM>).</P><P CLASS="Body"><A NAME="pgfId-1087528"></A><A NAME="marker-1087527"></A>Because everything is written to specification, all enterprise beans are interchangeable with containers, deployment tools, and servers created by other vendors. In fact, you might or might not write your own enterprise beans because it is possible, and sometimes desirable, to use enterprise beans written by one or more providers that you assemble into an Enterprise JavaBeans application.</P></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087529"></A>How Multitiered Applications Work</H5><P CLASS="Body"><A NAME="pgfId-1087531"></A><A NAME="marker-1087530"></A>The goal in a multitiered application is for the client to be able to work on application data without knowing at build time where the data is stored in the third tier. To make this level of transparency possible, the underlying services in a multitiered architecture use <A NAME="marker-1087532"></A>lookup services to locate remote server objects (the bean's remote interface object),<A NAME="marker-1087533"></A> and data <A NAME="marker-1087534"></A>communication services to move data from the client, through the remote server object, to its final destination in a storage medium. </P><P CLASS="Body"><A NAME="pgfId-1087535"></A><EM CLASS="Bold">Lookup Service. </EM>To find remote server objects at runtime, the client program needs a way to look them up. One way to look remote server objects up at runtime is to use the JNDI API. JNDI is a common interface to existing naming and directory interfaces. The <A NAME="marker-1087536"></A>Enterprise JavaBeans containers use JNDI as an interface to the RMI naming service. </P><P CLASS="Body"><A NAME="pgfId-1087538"></A><A NAME="marker-1087537"></A>At deployment time, the JNDI service registers (binds) the remote interface with a name. As long as the client program uses the same naming service and asks for the remote interface by its registered name, it will be able to find it. The client program calls the lookup method on a <EM CLASS="CODE">javax.naming.Context</EM><A NAME="marker-1087539"></A> object to ask for the remote interface by its registered name. The <EM CLASS="CODE">javax.naming.Context</EM> object stores the bindings; it is a different object from the Enterprise JavaBeans context, which is covered later in this book. </P><P CLASS="Body"><A NAME="pgfId-1087540"></A><EM CLASS="Bold">Data Communication. </EM>Once the client program gets a reference to a remote server object, it makes calls on the remote server object's methods. Because the client program has a reference to the remote server object, a technique called <A NAME="marker-1087541"></A><A NAME="marker-1087542"></A>data marshaling is used to make it appear as if the remote server object is local to the client program. </P><P CLASS="Body"><A NAME="pgfId-1087543"></A>In data marshaling, methods that are called on the remote server object are wrapped with their data and sent to the remote server object. The remote server object unwraps (unmarshals) the methods and data, and calls the enterprise bean. The results of the call to the enterprise bean are wrapped again, passed back to the client through the remote server object, and unmarshaled. </P><P CLASS="Body"><A NAME="pgfId-1087546"></A>The Enterprise JavaBeans containers use <A NAME="marker-1087544"></A><A NAME="marker-1087545"></A>RMI services to marshal data. When the bean is compiled, stub and skeleton files are created. The stub file provides the data wrapping and unwrapping configuration on the client, and the skeleton provides the same information for the server. The data is passed between the client program and the server using serialization. <A NAME="marker-1087547"></A>Serialization is a way to represent Java objects as bytes that can be sent over the network as a stream and reconstructed on the other side in the same state they were in when first sent. </P></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087549"></A><A NAME="94746"></A>How Enterprise Beans Are Used in the Example</H4><P CLASS="Body"><A NAME="pgfId-1087550"></A>The example uses two entity beans and two session beans. The entity beans, <EM CLASS="CODE">AuctionItemBean</EM> and <EM CLASS="CODE">RegistrationBean,</EM> represent <A NAME="marker-1087551"></A><A NAME="marker-1087552"></A>persistent items stored in a database, and the session beans, <EM CLASS="CODE">SellerBean</EM>, and <EM CLASS="CODE">BidderBean</EM>, represent short-lived operations with the client and data. </P><P CLASS="Body"><A NAME="pgfId-1087553"></A>The session beans are the client interface to the entity beans. The <EM CLASS="CODE">SellerBean</EM> object processes requests to add new auction items for sale. The <EM CLASS="CODE">BidderBean</EM> object processes requests to retrieve auction items and place bids on those items. Changing and adding to the database data in a container-managed bean is left to the entity beans. </P><P CLASS="Body"><A NAME="pgfId-1087554"></A>Entity and session beans are distributed objects that use the RMI API. This means that when an error occurs, an RMI remote exception is thrown. </P></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087556"></A><EM CLASS="A-code">AuctionServlet</EM><A NAME="12088"></A></H4><P CLASS="Body"><A NAME="pgfId-1087558"></A><EM CLASS="CODE">AuctionServlet </EM><A NAME="marker-1087557"></A>is essentially the second tier in the application and the focal point for auction activities. It accepts end-user input from the browser by way of hypertext transfer protocol (HTTP), passes the input to the appropriate enterprise bean for processing, and displays the processed results to the end user in the browser. </P><P CLASS="Body"><A NAME="pgfId-1087559"></A>Figure 2.4 presents a <EM CLASS="A">Unified Modeling Language (UML) </EM>class diagram for the <EM CLASS="CODE">AuctionServlet</EM> class. </P><DIV><H6 CLASS="FC"><A NAME="pgfId-1087564"></A>Figure 2.4 <A NAME="41984"></A>UML class diagram</H6><DIV><IMG SRC="CH02-4.gif"></DIV><P CLASS="Body"><A NAME="pgfId-1087565"></A>The <EM CLASS="CODE">AuctionServlet</EM> methods shown above invoke business logic that executes on the server by looking up an enterprise bean and calling one or more of its methods. When the servlet adds HTML codes to a page for display to the user, that logic executes on the client. </P><P CLASS="Body"><A NAME="pgfId-1087568"></A><A NAME="marker-1087566"></A>For example, the <EM CLASS="CODE">listAllItems(out)</EM><A NAME="marker-1087567"></A> method executes code on the client to dynamically generate an HTML page to be viewed by the client in a browser. The HTML page is populated with the results of a call to <EM CLASS="CODE">BidderBean</EM> that executes logic on the server to generate a list of all auction items. </P><PRE CLASS="CODE"><A NAME="pgfId-1087569"></A>private void listAllItems(ServletOutputStream out) throws IOException{//Put text on HTML page setTitle(out, "Auction results"); String text = "Click Item number for description and to place bid."; try{ addLine("<BR>"+ text, out);//Look up Bidder bean home interface. BidderHome bhome=(BidderHome) ctx.lookup("bidder");//Create Bidder bean remote interface. Bidder bid=bhome.create();//Call Bidder bean method through remote interface. Enumeration enum=(Enumeration)bid.getItemList(); if(enum != null) {//Put retrieved items on servlet page. displayitems(enum, out); addLine("", out); } } catch (Exception e) {//Print error on servlet page. addLine("AuctionServlet List All Items error",out); System.out.println("AuctionServlet <list>:"+e); } out.flush();}</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087571"></A><A NAME="87969"></A>Entity Bean Classes</H4><P CLASS="Body"><A NAME="pgfId-1087572"></A><EM CLASS="CODE">AuctionItemBean</EM> and <EM CLASS="CODE">RegistrationBean</EM> are entity beans. <EM CLASS="CODE">AuctionItemBean</EM> adds new auction items to the database and updates the bid amount as users bid on the item. <EM CLASS="CODE">RegistrationBean</EM> adds information to the database on registered users. Both beans consist of the classes described here. </P><DIV><H5 CLASS="B"><A NAME="pgfId-1087573"></A><EM CLASS="B-code">AuctionItem</EM> Entity Bean</H5><P CLASS="Body"><A NAME="pgfId-1087576"></A><A NAME="marker-1087574"></A>These are the <EM CLASS="CODE">AuctionItemBean</EM><A NAME="marker-1087575"></A> interfaces and classes:</P><UL><LI CLASS="BL"><A NAME="pgfId-1087577"></A> <EM CLASS="CODE">AuctionItem</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087578"></A><EM CLASS="A"> </EM><EM CLASS="CODE">AuctionItemHome</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087579"></A> <EM CLASS="CODE">AuctionItemBean</EM> </LI><LI CLASS="BL"><A NAME="pgfId-1087580"></A> <EM CLASS="CODE">AuctionItemPK</EM> </LI></UL><P CLASS="Body"><A NAME="pgfId-1087582"></A><EM CLASS="CODE">AuctionItem </EM><A NAME="marker-1087581"></A>is the remote interface. It describes what the bean does by declaring the developer-defined methods that provide the business logic for this bean. These methods are the ones used by the client to interact with the bean over the remote connection. Its name maps to the <EM CLASS="CODE">AUCTIONITEMS</EM> table shown just below. </P><PRE CLASS="CODE"><A NAME="pgfId-1087583"></A>package auction;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087584"></A> </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087585"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087586"></A>import java.rmi.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087587"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087588"></A> </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087589"></A>//Interface that contains the methods that can be called </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087590"></A>//in the auction beanpublic interface AuctionItem extends EJBObject {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087591"></A> String getDescription() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087592"></A> String getSeller() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087593"></A> int getId() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087594"></A> String getSummary() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087595"></A> double getIncrement() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087596"></A> double getHighBid() throws RemoteException; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087597"></A> int getBidCount() throws RemoteException; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087598"></A> String getHighBidder() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087599"></A> Date getStartDate() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087600"></A> Date getEndDate() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087601"></A> double getStartPrice() throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087602"></A> int setHighBid(String buyer, double amount, double increment) throws RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087603"></A>}</PRE><P CLASS="Body"><A NAME="pgfId-1087606"></A><EM CLASS="CODE">AuctionItemHome</EM><A NAME="62867"></A><A NAME="marker-1087605"></A> is the home interface. It describes how the bean is created in, found in, and removed from its container. The enterprise bean server deployment tools will provide the implementation for this interface. </P><PRE CLASS="CODE"><A NAME="pgfId-1087609"></A><A NAME="marker-1087607"></A><A NAME="marker-1087608"></A>package auction;</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -