Using Barcode maker for Java Control to generate, create UPC Code image in Java applications.

TarCode.com/UPC Symbol

Using Barcode generator for Java Control to generate, create GS1 DataBar-14 image in Java applications.

TarCode.com/GS1 DataBar Limited

as a stateful session bean is concerned, it is a good idea to perform a test that illustrates how an instance of that bean might be used within the duration of a certain client session. To achieve this, you might create two servlets that will be launched one after another so that the second servlet uses the CartBean instance created and used during the invocation of the first servlet. Diagrammatically, this might look like Figure 12-1.

Using Barcode creator for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.

TarCode.com/Code 3 of 9

Using Barcode drawer for Java Control to generate, create Universal Product Code version E image in Java applications.

TarCode.com/UPC - E1

Figure 12-1. The same CartBean instance can be used within the duration of a certain client session. Here are the tasks the first servlet might accomplish: 1. Obtains a CartBean bean instance and associates it with the HttpSession object, binding that bean instance to a session parameter 2. Adds a couple of items to the cart instance obtained in step 1 3. Iterates through the cart, displaying the items added in step 2 4. Only if an exception occurs, removes the cart instance from the session

Using Barcode printer for iPad Control to generate, create Data Matrix ECC200 image in iPad applications.

TarCode.com/Data Matrix 2d barcode

Using Barcode generator for Office Excel Control to generate, create Barcode image in Microsoft Excel applications.

TarCode.com/Barcode

CHAPTER 12 DESIGNING SESSION BEANS

and then bind it to the session. Finally, you invoke the cart s initialize method, passing the ID of a customer as the parameter.

Note JNDI lookup is used here because a servlet s instance may be shared by several clients simultaneously, thus making the use of dependency injection impossible.

Note that you remove the cart instance from the session only if an exception occurs. If this is not the case, the CartBean instance obtained in this servlet is not destroyed after servlet execution completion and stays available for further uses. It is interesting to note that if you execute the TestSampleServlet servlet discussed here two or more times, one after another, the second or subsequent execution will result in an error generated when calling the cart s addItem method. This is caused by the database server in response to the attempt to insert the same records into the shoppingCarts table. As a result, an exception will be thrown, and HttpSession s removeAttribute method will be called in the catch block, removing the cart instance from the session. In this situation, you don t need to worry about destroying the cart instance, since the container will do it for you. To avoid errors, however, you should not perform another execution of the TestSampleServlet servlet immediately after it has been executed. Instead, you should launch the TestSampleServletCont servlet that, among other things, will handle the task of destroying the CartBean instance obtained in the TestSampleServlet servlet earlier. Here are the tasks the TestSampleServletCont servlet might accomplish: 1. Obtains a CartBean bean instance and associates it with the HttpSession object, binding that bean instance to a session parameter 2. Removes one of those cart items added during the execution of the TestSampleServlet servlet discussed earlier 3. Iterates through the cart, displaying the items currently available in the cart 4. Empties the cart and destroys its instance 5. Removes the cart instance from the session Listing 12-5 shows what the source code for the TestSampleServletCont servlet might look like. Listing 12-5. Source Code for the TestSampleServletCont Servlet That Might Be Used to Test the CartBean Stateful Session Bean, When Called After the TestSampleServlet Servlet in Listing 12-4 package ejbjpa.servlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.ejb.EJB; import javax.naming.InitialContext; import java.util.List;