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 15-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 15-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
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, 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>/jwstutorial13/examples/web/bookstore1/ directory created when you unzip the tutorial bundle (see Building and Running the Examples). A sample bookstore1.war is provided in <INSTALL>/jwstutorial13/examples/web/provided-wars/. To build, package, deploy, and run the example:

  1. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  2. In a terminal window, go to <INSTALL>/jwstutorial13/examples/web/bookstore1/.
  3. Run ant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/jwstutorial13/examples/web/bookstore1/build/ directory.
  4. Start Tomcat.
  5. Perform all the operations described in Accessing Databases from Web Applications.
  6. Run ant install-config. The install-config target notifies Tomcat that the new context is available. See Installing Web Applications.
  7. To run the application, open the bookstore URL http://localhost:8080/bookstore1/bookstore.

To deploy the application:

  1. Run ant package. The package task creates a WAR file containing the application classes in WEB-INF/classes and the context.xml file in META-INF.
  2. Run ant deploy. The deploy target copies the WAR to Tomcat and notifies Tomcat that the new context is available.

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 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. Web log files reside in the directory <JWSDP_HOME>/logs and are named jwsdp_log.<date>.txt.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

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