Home TOC |
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.
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:
- Go to examples/src and build the example by running ant bookstore1 (See How to Build and Run the Examples).
- Start the Cloudscape database server by running cloudscape -start.
- Load the bookstore data into the database by running ant create-web-db.
- Start the j2ee server
- Start deploytool
- Create a J2EE application called bookstore1.
- Select File->New Application or the New Application button.
- Enter bookstore1.ear in the Application File Name field.
- Click OK.
- Add the banner web component and all of the Duke's Bookstore content to the bookstore1 application.
- Select File->New Web Component or the Web Component button.
- 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.
- Click Add to add the content files.
- 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.
- Click Next.
- Select the servlet radio button.
- Click Next.
- Select BannerServlet from the Servlet Class combo box.
- Type banner in the Web Component Name field.
- Click Next twice.
- In the Component Aliases panel click Add and then type /banner in the alias field.
- Click Finish.
- 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.
- Add a resource reference for the Cloudscape database.
- Select the bookstore1 WAR.
- Select the Resource Ref's tab.
- Click Add.
- Select javax.sql.DataSource from the Type column
- Enter jdbc/BookDB in the Coded Name field.
- Enter jdbc/Cloudscape in the JNDI Name field.
- Add the listener class listeners.ContextListener (described in Monitoring Servlet Life Cycle Events.
- Select the bookstore1 WAR.
- Select the Event Listeners tab.
- Click Add.
- Select the listeners.ContextListener class from drop down field in the Event Listener Classes panel.
- Add an error page (described in Handling Errors).
- Select the bookstore1 WAR.
- Select the File Ref's tab.
- Click Add in the Error Mapping panel.
- Enter exception.BookNotFoundException in the Error/Exception field.
- Enter /errorpage.html in the Resource to be Called field.
- Repeat for exception.BooksNotFoundException and javax.servlet.UnavailableException.
- Add the filters filters.HitCounterFilter and filters.OrderFilter (described in Filtering Requests and Responses).
- Select the bookstore1 WAR.
- Select the Filter Mapping tab.
- Click Edit Filter List.
- Click Add.
- Select the filter class filters.HitCounterFilter.
- Select the display name HitCounterFilter.
- Repeat for filters.OrderFilter.
- Click OK.
- Click Add.
- Select the filter name HitCounterFilter.
- Select URL Pattern for the Target Type.
- Enter the target /enter.
- Repeat for OrderFilter. The target type is URL Pattern and the target is /receipt.
- Deploy the application. Select Tools->Deploy Application or the Deploy Application Button. Enter the context root bookstore1.
- 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:
- 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 Cloudscape 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 Cloudscape 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 added the listener class to the application.
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>/weband 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 |
Search
Feedback |