Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

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 
Function
Servlet
Enter the bookstore
BookStoreServlet
Create the bookstore banner
BannerServlet
Browse the bookstore catalog
CatalogServlet
Put a book in a shopping cart
CatalogServlet,
BookDetailsServlet
Get detailed information on a specific book
BookDetailsServlet
Display the shopping cart
ShowCartServlet
Remove one or more books from the shopping cart
ShowCartServlet
Buy the books in the shopping cart
CashierServlet
Send an acknowledgment of the purchase
ReceiptServlet

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, package, deploy, and run the example using deploytool, 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.
  6. Start deploytool.
  7. Create a Web application called bookstore1 by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  8. In the New Web Component wizard:
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. In the WAR Location field, enter <INSTALL>/j2eetutorial14/examples/web/bookstore1/bookstore1.war.
    3. In the WAR Name field, enter bookstore1.
    4. In the Context Root field, enter /bookstore1.
    5. Click Edit Contents.
    6. 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.
    7. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and click Add.
    8. Click OK.
    9. Click Next.
    10. Select the Servlet radio button.
    11. Click Next.
    12. Select BannerServlet from the Servlet Class combo box.
    13. Click Finish.
  9. 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
      Alias
      BannerServlet
      BannerServlet
      /banner
      BookStoreServlet
      BookStoreServlet
      /bookstore
      CatalogServlet
      CatalogServlet
      /bookcatalog
      BookDetailsServlet
      BookDetailsServlet
      /bookdetails
      ShowCartServlet
      ShowCartServlet
      /bookshowcart
      CashierServlet
      CashierServlet
      /bookcashier
      ReceiptServlet
      ReceiptServlet
      /bookreceipt
  10. 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.
  11. 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.
  12. 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.
  13. 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 Display Name column.
    5. Click Add.
    6. Select filters.OrderFilter from the Filter Class column. deploytool will automatically enter OrderFilter in the Display Name column.
    7. Click OK.
    8. Click Add.
    9. Select HitCounterFilter from the Filter Name column.
    10. Select Servlet from the Target Type column.
    11. Select BookStoreServlet from the Target column.
    12. Repeat for OrderFilter. The target type is Servlet and the target is ReceiptServlet.
  14. 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.
  15. Select FileRight ArrowSave.
  16. 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.
  17. To run the application, open the bookstore URL http://localhost:8080/bookstore1/bookstore.

Troubleshooting

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.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.