The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

The Example Servlets

To illustrate servlet capabilities, this chapter uses an example application called Duke's Bookstore. The source for the application is located in examples/src/web/bookstore1. As shown in Table 9, each bookstore function is provided by a servlet. Different sections use the servlets to illustrate various tasks. For example, BookDetailsServlet illustrates how to handle HTTP GET requests and CatalogServlet shows you how to track session information.

Table 9 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 and 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 Cloudscape database and is 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.

To build, deploy, and run the example on the J2EE SDK:

  1. Go to examples/src and build the example by running ant bookstore1 (See How to Build and Run the Examples).
  2. Start the Cloudscape database server by running cloudscape -start.
  3. Load the bookstore data into the database by running ant create-web-db.
  4. Start the j2ee server
  5. Start deploytool
  6. Create a J2EE application called bookstore1.
    1. Select File->New Application or the New Application button.
    2. Enter bookstore1.ear in the Application File Name field.
    3. Click OK.
  7. Add the banner web component and all of the Duke's Bookstore content to the bookstore1 application.
    1. Select File->New Web Component or the Web Component button.
    2. Click the Create New WAR File in Application radio button and select the bookstore1 application from the combo box. Enter bookstore1 in the field labeled WAR Display Name.
    3. Click Add to add the content files.
    4. In the Edit Archive Contents dialog box, navigate to examples/build/web/bookstore1. Select BannerServlet.class, BookStoreServlet.class, BookDetailsServlet.class, CatalogServlet.class, ShowCartServlet.class, CashierServlet.class, and ReceiptServlet.class. Click Add. Add errorpage.html and duke.books.gif. Add the cart, database, exception, filters, listeners, and util packages. Click OK.
    5. Click Next.
    6. Select the servlet radio button.
    7. Click Next.
    8. Select BannerServlet from the Servlet Class combo box.
    9. Type banner in the Web Component Name field.
    10. Click Next twice.
    11. In the Component Aliases panel click Add and then type /banner in the alias field.
    12. Click Finish.
  8. Add each of the web components listed in Table 10. For each servlet, click the Add to Existing WAR radio button and select bookstore1 from the combo box.

    Table 10 Duke's Bookstore Web Components 
    Web Component Name
    Servlet Class
    Component Alias
    enter
    BookStoreServlet
    /enter
    catalog
    CatalogServlet
    /catalog
    bookdetails
    BookDetailsServlet
    /bookdetails
    showcart
    ShowCartServlet
    /showcart
    cashier
    CashierServlet
    /cashier
    receipt
    ReceiptServlet
    /receipt
  9. Add a resource reference for the Cloudscape database.
    1. Select the bookstore1 WAR.
    2. Select the Resource Ref's tab.
    3. Click Add.
    4. Select javax.sql.DataSource from the Type column
    5. Enter jdbc/BookDB in the Coded Name field.
    6. Enter jdbc/Cloudscape in the JNDI Name field.
  10. Add the listener class listeners.ContextListener (described in Monitoring Servlet Life Cycle Events.
    1. Select the bookstore1 WAR.
    2. Select the Event Listeners tab.
    3. Click Add.
    4. Select the listeners.ContextListener class from drop down field in the Event Listener Classes panel.
  11. Add an error page (described in Handling Errors).
    1. Select the bookstore1 WAR.
    2. Select the File Ref's tab.
    3. Click Add in the Error Mapping panel.
    4. Enter exception.BookNotFoundException in the Error/Exception field.
    5. Enter /errorpage.html in the Resource to be Called field.
    6. Repeat for exception.BooksNotFoundException and javax.servlet.UnavailableException.
  12. Add the filters filters.HitCounterFilter and filters.OrderFilter (described in Filtering Requests and Responses).
    1. Select the bookstore1 WAR.
    2. Select the Filter Mapping tab.
    3. Click Edit Filter List.
    4. Click Add.
    5. Select the filter class filters.HitCounterFilter.
    6. Select the display name HitCounterFilter.
    7. Repeat for filters.OrderFilter.
    8. Click OK.
    9. Click Add.
    10. Select the filter name HitCounterFilter.
    11. Select URL Pattern for the Target Type.
    12. Enter the target /enter.
    13. Repeat for OrderFilter. The target type is URL Pattern and the target is /receipt.
  13. Deploy the application. Select Tools->Deploy Application or the Deploy Application Button. Enter the context root bookstore1.
  14. Open the bookstore URL http://<host>:8000/bookstore1/enter.

Troubleshooting

Common Problems and Their Solutions (in particular Web Client Runtime Errors) lists some reasons why a web application can fail. In addition, Duke's Bookstore returns the following exceptions:

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 Occurred and 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:

     $J2EE_HOME/<logs>/<host>/web 

and are named catalina.<date>.log.

The <logs> element is the directory specified by the log.directory entry in the default.properties file. The default value is logs. The <host> element is the name of the computer. See the Configuration Guide provided with the J2EE SDK for more information about J2EE SDK log files.

Home
TOC
PREV TOC NEXT Search
Feedback