Using the Rental Web Application


After building the rental Web application and starting Tomcat, you are ready to use the application. Using a Web browser, go to the following URL:

http://localhost:8080/rental/controller

This will bring up a Web page that looks something like the one in Figure 10-1. Notice the table, which has a row for each week that rentals are available and a column for each lighthouse that can be rented.

click to expand
Figure 10-1: The rental Web page when the customer is unknown

When you first visit the site, the application does not know who you are. To follow along with the tests described here, enter the customer name "Jim," and click the change customer button. This action redisplays the page, which now looks something like the page shown in Figure 10-2. The available rentals now have enabled check boxes. (Some browsers do not permit the check box to be disabled, so you may have seen enabled check boxes earlier.) In addition, there is now a button for submitting reservation changes.

click to expand
Figure 10-2: The rental Web page when the customer is Jim

Reserve the Nubble lighthouse for Jim by checking the box for the first week and clicking the submit reservation changes button. This will display a page showing Jim's reservations. Using another browser, identify the customer as Mary, and reserve for Mary the Nubble lighthouse in the second week.

Starting Up Two Browser Clients

By using a different browser (as opposed to another window in the same browser), you ensure that the Web application sees two clients rather than one. If you do not have another browser to use, you will have to experiment to see whether two windows in your browser are treated by the servlet container as one client or two clients. One way to perform this experiment is to use the test JSP for the session lock that is automatically created by the rental-servlet-opr build target. In each of the browser windows, go to the following URL:

http://localhost:8080/rental/SessionLock.jsp

If the servlet session that the browser window is using is new, you will see in the browser window text that looks like the following:

 This session is new. This session was last accessed 0.0 seconds ago. This lock has been called 1 times. 

Otherwise, you will see output that looks something like the following:

 This session is not new. This session was last accessed 54.489 seconds ago. This lock has been called 3 times. 

Encountering Conflicting Reservations in the Rental Web Client

After bringing up two browser windows that are using distinct servlet sessions, follow these steps to see an optimistic lock conflict and its resolution. In the browser that Jim is using, change the view to Both. Do the same for Mary. At this point, you should see Web pages that look like the Web pages in Figures 10-3 and 10-4. In Figure 10-3, Jim sees one reservation made in his name, and one rental period that is not available because Mary has it. In Figure 10-4, Mary sees one reservation made in her name, and one rental period that is not available because Jim has it.

click to expand
Figure 10-3: Page showing Jim about to cancel one reservation and make another

click to expand
Figure 10-4: Page showing Mary about to cancel one reservation and make another

To observe the optimistic concurrency check in action, perform the following test. Take care to perform the steps in the order indicated; otherwise, you might accidentally refresh the view before you request the update. In Jim's browser, cancel his reservation and reserve the third week for Nubble. Click the submit reservation changes button. As a result, Jim has cancelled his reservation for the first week and made a reservation for the third week. Now go to Mary's browser. Cancel her reservation for the second week and reserve the third week for Nubble. After clicking the submit reservation changes button, Mary's browser will display the error message shown in Figure 10-5. The error arises when the ReservationServlet detects that the version of the Rental object for Nubble in the third week that Mary looked at in her view request is older than the version that the ReservationServlet was about to change in her update request.

click to expand
Figure 10-5: Page showing an error after Mary attempted to reserve Nubble for the third week

After clicking the continue button, Mary sees the reservation page that looks something like the page shown in Figure 10-6. Note that her reservation in the second week is not cancelled and that she now sees that the first week for Nubble is available and the third week is not available. In other words, her transaction was rolled back and her view was refreshed.

click to expand
Figure 10-6: Mary's Web page after recovering from the error




Using and Understanding Java Data Objects
Using and Understanding Java Data Objects
ISBN: 1590590430
EAN: 2147483647
Year: 2005
Pages: 156
Authors: David Ezzio

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