The Example Servlets

This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 11-1 lists the servlets that handle each bookstore function. Each programming task is illustrated by one or more servlets. For example, BookDetailsServlet illustrates how to handle HTTP GET requests, BookDetailsServlet and CatalogServlet show how to construct responses, and CatalogServlet illustrates how to track session information.

Table 11-1 Duke's Bookstore Example Servlets 
Enter the bookstore
Create the bookstore banner
Browse the bookstore catalog
Put a book in a shopping cart
Get detailed information on a specific book
Display the shopping cart
Remove one or more books from the shopping cart
Buy the books in the shopping cart
Send an acknowledgment of the purchase

The data for the bookstore application is maintained in a database and accessed through the database access class database.BookDBAO. The database package also contains the class BookDetails, which represents a book. The shopping cart and shopping cart items are represented by the classes cart.ShoppingCart and cart.ShoppingCartItem, respectively.

The source code for the bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore1/ directory, which is created when you unzip the tutorial bundle (see Building the Examples). A sample bookstore1.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build the application, follow these steps:

  1. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  2. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore1/.
  3. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore1/build/ directory.
  4. Start the Application Server.
  5. Perform all the operations described in Accessing Databases from Web Applications.

To package and deploy the example using asant, follow these steps:

  1. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore1/.
  2. Run asant create-bookstore-war.
  3. Run asant deploy-war.

To learn how to configure the example, use deploytool to package and deploy it:

  1. Start deploytool.
  2. Create a web application called bookstore1 by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  3. In the New Web Component wizard:
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. In the WAR File field, enter <INSTALL>/j2eetutorial14/examples/web/bookstore1/bookstore1.war. The WAR Display Name field will show bookstore1.
    3. In the Context Root field, enter /bookstore1.
    4. Click Edit Contents.
    5. In the Edit Archive Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore1/build/. Select errorpage.html, duke.books.gif, and the servlets, database, filters, listeners, and util packages. Click Add.
    6. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore/dist/. Select bookstore.jar and click Add.
    7. Click OK.
    8. Click Next.
    9. Select the Servlet radio button.
    10. Click Next.
    11. Select BannerServlet from the Servlet Class combo box.
    12. Click Finish.
  4. Add the rest of the web components listed in Table 11-2. For each servlet:
    1. Select FileRight ArrowNewRight ArrowWeb Component.
    2. Click the Add to Existing WAR Module radio button. Because the WAR contains all the servlet classes, you do not have to add any more content.
    3. Click Next.
    4. Select the Servlet radio button.
    5. Click Next.
    6. Select the servlet from the Servlet Class combo box.
    7. Click Finish.
      Table 11-2 Duke's Bookstore Web Components 
      Web Component Name
      Servlet Class
  5. Set the alias for each web component.
    1. Select the component.
    2. Select the Aliases tab.
    3. Click the Add button.
    4. Enter the alias.
  6. Add the listener class listeners.ContextListener (described in Handling Servlet Life-Cycle Events).
    1. Select the Event Listeners tab.
    2. Click Add.
    3. Select the listeners.ContextListener class from the drop-down field in the Event Listener Classes pane.
  7. Add an error page (described in Handling Errors).
    1. Select the File Ref's tab.
    2. In the Error Mapping pane, click Add Error.
    3. Enter exception.BookNotFoundException in the Error/Exception field.
    4. Enter /errorpage.html in the Resource to be Called field.
    5. Repeat for exception.BooksNotFoundException and javax.servlet.UnavailableException.
  8. Add the filters filters.HitCounterFilter and filters.OrderFilter (described in Filtering Requests and Responses).
    1. Select the Filter Mapping tab.
    2. Click Edit Filter List.
    3. Click Add Filter.
    4. Select filters.HitCounterFilter from the Filter Class column. deploytool will automatically enter HitCounterFilter in the Filter Name column.
    5. Click Add Filter.
    6. Select filters.OrderFilter from the Filter Class column. deploytool will automatically enter OrderFilter in the Filter Name column.
    7. Click OK.
    8. Click Add.
    9. Select HitCounterFilter from the Filter Name drop-down menu.
    10. Select the Filter this Servlet radio button in the Filter Target frame.
    11. Select BookStoreServlet from the Servlet Name drop-down menu.
    12. Click OK.
    13. Repeat for OrderFilter. Select ReceiptServlet from the Servlet Name drop-down menu.
  9. Add a resource reference for the database.
    1. Select the Resource Ref's tab.
    2. Click Add.
    3. Enter jdbc/BookDB in the Coded Name field.
    4. Accept the default type javax.sql.DataSource.
    5. Accept the default authorization Container.
    6. Accept the default selected Shareable.
    7. Enter jdbc/BookDB in the JNDI name field of the Sun-specific Settings frame.
  10. Select FileRight ArrowSave.
  11. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. In the Connection Settings frame, enter the user name and password you specified when you installed the Application Server.
    3. Click OK.

To run the application, open the bookstore URL http://localhost:8080/bookstore1/bookstore.


The Duke's Bookstore database access object returns the following exceptions:

Because we have specified an error page, you will see the message

The application is unavailable. Please try later.  

If you don't specify an error page, the web container generates a default page containing the message

A Servlet Exception Has Occurred  

and a stack trace that can help you diagnose the cause of the exception. If you use errorpage.html, you will have to look in the server log to determine the cause of the exception.