PreviousHomeNext Search

The Example Servlets

This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 3-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 3-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
Receive an acknowledgement for the purchase

The data for the bookstore application is maintained in a database and accessed through the helper class database.BookDB. 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 for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial/examples/web/bookstore1 directory created when you unzip the tutorial bundle (see About the Examples).

To deploy and run the example:

  1. In the IDE, mount the filesystem
  2. Expand the bookstore1 node.
  3. Right-click the WEB-INF directory and choose Deploy.
  4. Set up the PointBase database as described in Accessing Databases from Web Applications.
  5. Open the bookstore URL http://localhost:80/bookstore1/enter.

To review the deployment settings:

  1. Expand the WEB-INF node.
  2. Select the web.xml file.
  3. Select the Deployment property sheet.
  4. Browse the error page mappings.
    1. Click the Error Pages property and open the property editor.
    2. Notice that two HTTP status codes (404 and 500) and four exceptions are mapped to the error page /errorpage.html.
  5. Browse the filter property and mappings.
    1. Click the Filters property and open the property editor.
    2. Notice that the filters classes are filters.HitCounterFilter and filters.OrderFilter and they are mapped to BookStoreServlet and ReceiptServlet respectively.
  6. Browse the listener property.
    1. Click the Listeners property and open the property editor.
    2. Notice that the listener class is listeners.ContextListener.
  7. Browse the servlet definitions and their mappings.
    1. Click the Servlets property and open the property editor.
    2. Notice that there are 7 servlets listed defined and mapped to URLs.
  8. Browse the resource references.
    1. Select the Resources Property sheet.
    2. Click the Resource References property and open the property editor.
    3. Note the resource reference named jdbc/BookDB.


Because we have specified an error page, you will see the message The application is unavailable. Please try later, when an exception occurs. You will have to look in the application server's log to determine the cause of the exception. The log file is <S1AS7_HOME>/domains/domain1/server1/logs/server.log. You can view the log in the application server administration tool as follows:

  1. Start the administration console by opening the URL http://localhost:4848 in a browser.
  2. Select the server1 node.
  3. Select the Logging tab.
  4. Click the View Event Log link.

Here are the exceptions that can be returned and their causes:

PreviousHomeNext Search

All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.