The Example Servlets



The Example Servlets

This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Figure 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 shows you how to track session information.

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

Receive an acknowledgement for the purchase

ReceiptServlet

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 that represents a book. The shopping cart and shopping cart items are represented by the classes cart.ShoppingCart and cart.ShoppingCartItem.

The source for the bookstore application is located in the docs/tutorial/examples/web/bookstore1 directory created when you unzip the tutorial bundle (see Running the Examples (page xx)). To build, deploy, and run the example:

  1. Go to the bookstore1 directory and build and deploy the example by running ant. This runs the default ant target deploy which depends on the build target. The build target will spawn any necessary compilations and copy files to the docs/tutorial/examples/web/bookstore1/build directory. The deploytarget copies the bookstore1 context file to <JWSDP_HOME> /webapps as described in Running Web Applications (page 360).

  2. Start the Pointbase database server (see Accessing Databases from Web Applications (page 363)).

  3. Start or restart Tomcat.

  4. Open the bookstore URL http://localhost:8080/bookstore1/enter.

1 Troubleshooting

Common Problems and Their Solutions (page 65) lists some reasons why a Web client can fail. In addition, Duke's Bookstore returns the following exceptions:

  • BookNotFoundException— if a book can't be located in the bookstore database. This will occur if you haven't loaded the bookstore database with data by running ant create-web-db or if the database server hasn't been started or it has crashed.

  • BooksNotFoundException— if the bookstore data can't be retrieved. This will occur if you haven't loaded the bookstore database with data by running ant create-web-db or if the database server hasn't been started or it has crashed.

  • UnavailableException— if a servlet can't retrieve the Web context attribute representing the bookstore. This will occur if you haven't copied the Pointbase client library <PB_HOME> /client/lib/pbclient41ev.jarto <JWSDP_HOME> /common/lib.

Since 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 Occurredand a stack trace that can help diagnose the cause of the exception. If you use the errorpage.html, you will have to look in the Web container's log to determine the cause of the exception. Web log files reside in the directory <JWSDP_HOME> /logsand are named jwsdp_log.<date> .txt.