4.1 When to Use Session Beans

A session bean is a particular type of enterprise bean used to implement a communication session between a client and a server. A session object lasts for the duration of the client's session. This means that a session is defined as the time between the creation of the session bean and its removal. A stateful session object stores the state information, or conversational state, for the client that creates and uses the session bean. A stateless session bean does not store any conversational state on behalf of a client. Section 4.2.1, Stateful versus Stateless Session Beans, on page 69 discusses the differences between stateless and stateful session beans.

A session bean typically implements a business process performed from start to end by a single actor in a single session, the scope of which is referred to as a conversation. (For an in-depth discussion of conversational state, refer to Section 4.2.2, Understanding Conversational State, on page 71.) For example, session beans are suitable in the following two situations:

  1. To implement the session logic of Web applications. One or more session beans can usually implement the business process that drives the conversation with a user. For example, a session bean can implement the business process to enter a customer's shipping and billing information.

  2. To implement the session logic of traditional (non-Web-based) three-tier applications. For example, a session bean can implement the business process performed by a bank teller when entering multiple checks on behalf of a customer.

These two cases are discussed in more detail in the following sections. In addition, stateless session beans are used when an enterprise application needs to expose itself as a Web service. This is discussed in Chapter 9.

4.1.1 Session Beans in Web Applications

A session bean is well suited for the implementation of a business process that drives the conversation between a Web application and a user accessing the application through a browser. Figure 4.1 illustrates the segments of a Web application and shows the session bean's position within the business logic segment.

Figure 4.1. Session Beans in a Web Application

graphics/04fig01.gif

The user's browser exchanges information with the Web application in the HTML format by using the HyperText Transfer Protocol (HTTP). The Web application, which is a collection of JSPs and servlets deployed in the Web container, processes the HTTP requests. The Web application is responsible for processing the requests sent by the user's browser and for generating the HTML pages that are sent back to the browser in reply. To accomplish this, the Web application invokes one or more session beans, which process the data sent by the user and generate the data that is formatted usually by the JSP or servlet into the HTML page sent as the reply.

The Web application architecture that is, the servlet and JSP specifications define the concept of an HTTP session. An HTTP session spans multiple HTTP requests from the user; the Web application controls the start and end of an HTTP session. The Web container supports the HTTP session concept to allow the Web application to retain session-specific state across multiple requests from the same user.

Although it is possible to embed the implementation of a business process directly into the Web application, it is preferable to encapsulate the business process into a session bean. The encapsulation of the business process makes it possible to reuse the business process with a different presentation interface, such as a Palm Pilot or touch-tone phone interface. See Section 2.2, Business Entities and Processes, and Enterprise Bean Types, on page 20, for more information about business processes.

4.1.2 Session Beans in Traditional Three-Tier Applications

The traditional three-tier application implements the presentation logic of the application as a Java client application that runs directly on the user's PC. The client application implements the GUI for the business process. Driven by the user's input, the client application interacts over the corporate network with the session bean that implements the business processes. Figure 4.2 illustrates how session beans fit within a three-tier application.

Figure 4.2. Session Beans in a Traditional Three-Tier Application

graphics/04fig02.gif

Session beans simplify the design and implementation of the three-tier application by shifting to the EJB container most of the complexities resulting from the distributed application design. Session beans often enable an existing two-tier application to be easily converted to a three-tier architecture by allowing the designer to separate the code that implements the business process from the code that implements the GUI. The application designer packages the code that implements the business process as a session bean. The client application implementing the presentation logic can be implemented, for example, as a Swing application. (A Swing application uses the GUI components provided by J2SE.)



Applying Enterprise Javabeans
Applying Enterprise JavaBeans(TM): Component-Based Development for the J2EE(TM) Platform
ISBN: 0201702673
EAN: 2147483647
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net