⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><META NAME="GENERATOR" CONTENT="Adobe FrameMaker 6.0/HTML Export Filter"><LINK REL="STYLESHEET" HREF="CH02.css" CHARSET="ISO-8859-1" TYPE="text/css"><TITLE> Covered in this Chapter</TITLE></HEAD><BODY BGCOLOR="#ffffff"><P CLASS="CT"><A NAME="pgfId-1087400"></A>2<A NAME="46038"></A></P><P CLASS="CT"><A NAME="pgfId-1087401"></A>Auction House Application</P><P CLASS="Body"><A NAME="pgfId-1087402"></A>The proliferation of Internet- and intranet-based applications has created a great need for distributed transactional applications that leverage the speed, security, and reliability of server-side technology. One way to meet this need is to use a multitiered model in which a thin client application invokes business logic that executes on the server. <A NAME="marker-1087403"></A><A NAME="marker-1087404"></A>Normally, thin client multitiered applications are hard to write because they involve many lines of intricate code to handle transaction and state management, multithreading, resource pooling, and other complex low-level details. And to add to the difficulties, you have to rework this intricate code every time you write a new application because the code is so low-level it cannot be reused. </P><P CLASS="Body"><A NAME="pgfId-1087405"></A>If you could use prebuilt and pretested transaction management code or even reuse some of your own code, you would save a lot of time and energy that you could better spend solving the business problem. Well, Enterprise JavaBeans technology can give you the help you need. <A NAME="marker-1087406"></A><A NAME="marker-1087407"></A>Enterprise JavaBeans technology makes distributed transactional applications easy to write because it separates the low-level details from the business logic. You concentrate on creating the best business solution and leave the rest to the underlying architecture. </P><P CLASS="Body"><A NAME="pgfId-1087408"></A>This chapter describes how to create an example auction with the services provided by the Enterprise JavaBeans platform. Later chapters show how to customize these services. </P><DIV><H4 CLASS="A"><A NAME="pgfId-1087409"></A>Covered in this Chapter</H4><UL><LI CLASS="BL"><A NAME="pgfId-1087410"></A>A Multitiered Application with Enterprise Beans (page 12) </LI><LI CLASS="BL"><A NAME="pgfId-1087411"></A>How Enterprise Beans Are Used in the Example (page 17) </LI><LI CLASS="BL"><A NAME="pgfId-1087412"></A>AuctionServlet (page 18)</LI><LI CLASS="BL"><A NAME="pgfId-1087413"></A>Entity Bean Classes (page 19) </LI><LI CLASS="BL"><A NAME="pgfId-1087414"></A>Session Bean Classes (page 22)</LI><LI CLASS="BL"><A NAME="pgfId-1087415"></A>Container Classes (page 24)</LI><LI CLASS="BL"><A NAME="pgfId-1087416"></A>Examining a Container-Managed Bean (page 24) </LI><LI CLASS="BL"><A NAME="pgfId-1087417"></A>Container-Managed Finder Methods (page 27)</LI></UL></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087419"></A><A NAME="97785"></A>A Multitiered Application with Enterprise Beans</H4><P CLASS="Body"><A NAME="pgfId-1087421"></A>An <A NAME="marker-1087420"></A>enterprise bean is a small set of interfaces and classes that provide two types of methods: business logic and life cycle. A client program calls the business logic methods to interact with the data on the server. Every enterprise bean has a system-level container that calls the life-cycle methods to manage the bean on the server. In addition to these two types of methods, an enterprise bean has an associated configuration file, called a deployment descriptor, to configure the bean at deployment time. </P><P CLASS="Body"><A NAME="pgfId-1087422"></A>As well as being responsible for creating and deleting beans, the Enterprise JavaBeans server also manages transactions, concurrency, security, and data persistence (storing and retrieving data). Even the connections between the client and server are provided using the Remote Method Invocation (RMI) and Java Naming and Directory Interface? (JNDI) APIs, and servers can optionally provide scalability through thread management and caching. </P><P CLASS="Body"><A NAME="pgfId-1087424"></A>The <A NAME="marker-1087423"></A>auction house example implements a complete Enterprise JavaBeans solution by providing only the business logic and using the underlying services provided by the architecture. However, you may find that the container-managed services, although providing maximum portability, do not meet all your application requirements. The next chapters show how to provide these services in your bean<EM CLASS="A">.</EM></P><DIV><H5 CLASS="B"><A NAME="pgfId-1087425"></A>Thin Client Programs and Multitiered Architecture</H5><P CLASS="Body"><A NAME="pgfId-1087427"></A>A <A NAME="marker-1087426"></A>thin client is a client program that invokes business logic running on the server. It is called <EM CLASS="EM">thin</EM> because most of the processing happens on the server. The auction house application shown in Figure 2.1 has a user interface that is a set of HTML pages. The HTML pages get input from and show information to the user in the browser. Behind the HTML pages is a <A NAME="marker-1087428"></A><A NAME="marker-1087429"></A>servlet that passes data between the browser and the Enterprise JavaBeans server. The Enterprise JavaBeans server handles reading from and writing to the underlying database.</P><DIV><H6 CLASS="FC"><A NAME="pgfId-1087434"></A>Figure 2.1 <EM CLASS="A">Client and server sides</EM><A NAME="27363"></A></H6><DIV><IMG SRC="CH02-1.gif"></DIV><P CLASS="Body"><A NAME="pgfId-1087436"></A><A NAME="marker-1087435"></A>Multitiered architecture or three-tier architecture extends the standard two-tier client-and-server model by placing a multithreaded application server between the client and the database. The application server is where the enterprise beans reside and where the application's business logic executes. </P><P CLASS="Body"><A NAME="pgfId-1087437"></A>Figure 2.2 shows that client programs (on the first tier) communicate with the database (third tier) through the <A NAME="marker-1087438"></A><A NAME="marker-1087439"></A>application server (second tier). The application server responds to the client requests and <A NAME="marker-1087440"></A><A NAME="marker-1087441"></A>makes database calls as needed into the underlying database.<A NAME="es"></A> </P></DIV><DIV><H6 CLASS="FC"><A NAME="pgfId-1087447"></A>Figure 2.2 <A NAME="62232"></A>Multitiered communications</H6><DIV><IMG SRC="CH02-2.gif"></DIV></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087448"></A>Entity and Session Bean Differences</H5><P CLASS="Body"><A NAME="pgfId-1087449"></A>There are two types of enterprise beans: entity beans and session beans. A session bean is created on behalf of a client and usually exists only for the duration of a single client and server session. A session bean performs operations, such as calculations or accessing a database, for the client. The data in a session bean is not recoverable in the event its container crashes. </P><P CLASS="Body"><A NAME="pgfId-1087450"></A>An entity bean is a persistent object that represents data maintained in a database. An entity bean can manage its own persistence, or it can delegate this function to its container. An entity bean can live as long as the data it represents. An entity bean is identified by a primary key. If the container in which an entity bean is hosted crashes, the entity bean, its primary key, and any remote references survive the crash.</P><P CLASS="Body"><A NAME="pgfId-1087452"></A><A NAME="marker-1087451"></A>Typically, an entity bean represents one row of persistent data stored in a database table. In the auction house example, <EM CLASS="CODE">RegistrationBean</EM> is an entity bean that represents data for one registered user, and <EM CLASS="CODE">AuctionItemBean</EM> is an entity bean that represents the data for one auction item. Entity beans are transactional and long-lived. As long as the data remains, the entity bean can access and update that data. This does not mean you need a bean running for every table row. Instead, entity beans are loaded and saved as needed. </P><P CLASS="Body"><A NAME="pgfId-1087454"></A><A NAME="marker-1087453"></A>A session bean might execute database reads and writes, but it is not required. A session bean might invoke JDBC calls itself or it might use an entity bean to make the call, in which case the session bean is a client to the entity bean. A session bean's fields contain the state of the conversation and are transient (are not saved). If the server or client crashes, the session bean is gone. A session bean is often used with one or more entity beans to perform complex operations on the data. </P><TABLE><TR><TH ROWSPAN="1" COLSPAN="1"><P CLASS="TCH"><A NAME="pgfId-1087457"></A>Session Beans</P></TH><TH ROWSPAN="1" COLSPAN="1"><P CLASS="TCH"><A NAME="pgfId-1087459"></A>&nbsp;</P></TH><TH ROWSPAN="1" COLSPAN="1"><P CLASS="TCH"><A NAME="pgfId-1087461"></A>Entity Beans</P></TH></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB1"><A NAME="pgfId-1087463"></A>Fields contain conversation state. </P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB1"><A NAME="pgfId-1087465"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB1"><A NAME="pgfId-1087467"></A>Represents data in a database.</P></TD></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087469"></A>Handles database access for client.         </P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087471"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087473"></A>Shares access for multiple users.</P></TD></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087475"></A>Life of client is life of Bean.</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087477"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087479"></A>Persists as long as data exists.</P></TD></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087481"></A>Can be transaction aware. </P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087483"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087485"></A>Transactional.</P></TD></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087487"></A>Does not survive server crashes. </P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087489"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087491"></A>Survives server crashes.</P></TD></TR><TR><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB-rule"><A NAME="pgfId-1087493"></A>No fine-grained data handling. </P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB-rule"><A NAME="pgfId-1087495"></A>&nbsp;</P></TD><TD ROWSPAN="1" COLSPAN="1"><P CLASS="TB"><A NAME="pgfId-1087497"></A>Fine-grained data handling.</P></TD></TR></TABLE><UL><P CLASS="NOTE"><A NAME="pgfId-1087498"></A>NOTE In the Enterprise JavaBeans specification, Enterprise JavaBeans server support for session beans is mandatory. Enterprise JavaBeans server support for entity beans is optional, but is mandatory for version 2.0 of the specification.</P></UL></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087499"></A>Auction House Workings</H5><P CLASS="Body"><A NAME="pgfId-1087501"></A><A NAME="marker-1087500"></A>Figure 2.3 shows the enterprise beans for the auction house application and their relationship to the Enterprise JavaBeans server. The thin-client server invokes business logic in the four enterprise beans through their home and remote interfaces (described below). The Enterprise JavaBeans server in this example handles the low-level details including database read and write operations. The four enterprise beans in the example can be described as follows: </P><DIV><H6 CLASS="FC"><A NAME="pgfId-1087506"></A>Figure 2.3 <A NAME="54200"></A>Parts of a Bean</H6><DIV><IMG SRC="CH02-3.gif"></DIV><UL><LI CLASS="BL"><A NAME="pgfId-1087508"></A><EM CLASS="CODE">AuctionItemBean</EM><A NAME="marker-1087507"></A> is an entity bean that maintains information for an auction item.</LI><LI CLASS="BL"><A NAME="pgfId-1087510"></A><EM CLASS="CODE">RegistrationBean</EM><A NAME="marker-1087509"></A> is an entity bean that stores user registration information. </LI>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -