|
Home TOC Index |
|
Search
Feedback |
The Example Servlets
This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 10-1 lists the servlets that handle each bookstore function. Each programming task is illustrated by one or more servlets. For example,
BookDetailsServletillustrates how to handle HTTPGETrequests,BookDetailsServletandCatalogServletshow how to construct responses, andCatalogServletillustrates how to track session information.The data for the bookstore application is maintained in a database and accessed through the helper class
database.BookDB. Thedatabasepackage also contains the classBookDetails, which represents a book. The shopping cart and shopping cart items are represented by the classescart.ShoppingCartandcart.ShoppingCartItem, respectively.The source code for the bookstore application is located in the
j2eetutorial/examples/src/web/bookstore1directory created when you unzip the tutorial bundle (see Downloading the Examples). To build, deploy, and run the example, follow these steps.
- Go to
j2eetutorial/examplesand build the example by runningant bookstore1(see How to Build and Run the Examples).- Start the
j2eeserver.- Start
deploytool.- Start the Cloudscape database server by running
cloudscape -start.- Load the bookstore data into the database by running
ant create-web-db.- Create a J2EE application called
Bookstore1App.
- Select File
New
Application.
- In the file chooser, navigate to
j2eetutorial/examples/src/web/bookstore1.- In the File Name field, enter
Bookstore1App.- Click New Application.
- Click OK.
- Create the WAR and add the
BannerServletWeb component and all of the Duke's Bookstore content to theBookstore1Appapplication.
- Select File
New
Web Component.
- Click the Create New WAR File In Application radio button and select
Bookstore1Appfrom the combo box. EnterBookstore1WARin the field labeled WAR Display Name.- Click Edit to add the content files.
- In the Edit Archive Contents dialog box, navigate to
j2eetutorial/examples/build/web/bookstore1. SelectBannerServlet.class,BookStoreServlet.class,BookDetailsServlet.class,CatalogServlet.class,ShowCartServlet.class,CashierServlet.class, andReceiptServlet.class. Click Add. Adderrorpage.htmlandduke.books.gif. Add thecart,database,exception,filters,listeners,messages, andutilpackages. Click OK.- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select
BannerServletfrom the Servlet Class combo box.- Click Next twice.
- In the Component Aliases pane, click Add and then type
/bannerin the Alias field.- Click Finish.
- Add each of the Web components listed in Table 10-2. For each servlet, click the Add to Existing WAR File radio button and select
Bookstore1WARfrom the combo box. Since the WAR contains all of the servlet classes, you do not have to add any more content.- Add a resource reference for the Cloudscape database.
- Select
Bookstore1WAR.- Select the Resource Refs tab.
- Click Add.
- Select
javax.sql.DataSourcefrom the Type column- Enter
jdbc/BookDBin the Coded Name field.- Enter
jdbc/Cloudscapein the JNDI Name field.- Add the listener class
listeners.ContextListener(described in Handling Servlet Life-Cycle Events.
- Select the Event Listeners tab.
- Click Add.
- Select the
listeners.ContextListenerclass from the drop-down field in the Event Listener Classes pane.- Add an error page (described in Handling Errors).
- Select the File Refs tab.
- In the Error Mapping panel, click Add.
- Enter
exception.BookNotFoundExceptionin the Error/Exception field.- Enter
/errorpage.htmlin the Resource To Be Called field.- Repeat for
exception.BooksNotFoundExceptionandjavax.servlet.UnavailableException.- Add the filters
filters.HitCounterFilterandfilters.OrderFilter(described in Filtering Requests and Responses).
- Select the Filter Mapping tab.
- Click Edit Filter List.
- Click Add.
- Select
filters.HitCounterFilterfrom the Filter Class column. Thedeploytoolutility will automatically enterHitCounterFilterin the Display Name column.- Click Add.
- Select
filters.OrderFilterfrom the Filter Class column. Thedeploytoolutility will automatically enterOrderFilterin the Display Name column.- Click OK.
- Click Add.
- Select
HitCounterFilterfrom the Filter Name column.- Select Servlet from the Target Type column.
- Select
BookStoreServletfrom the Target column.- Repeat for
OrderFilter. The target type is Servlet and the target isReceiptServlet.- Enter the context root.
- Deploy the application.
- Open the bookstore URL
http://<host>:8000/bookstore1/enter.Troubleshooting
The section Common Problems and Their Solutions (in particular, Web Client Runtime Errors) lists some reasons why a Web client can fail. In addition, Duke's Bookstore returns the following exceptions:
BookNotFoundException: Returned 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 runningant create-web-dbor if the Cloudscape server hasn't been started or it has crashed.BooksNotFoundException: Returned if the bookstore data can't be retrieved. This will occur if you haven't loaded the bookstore database with data by runningant create-web-dbor if the Cloudscape server hasn't been started or has crashed.UnavailableException: Returned 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 messageA Servlet Exception Has Occurredand a stack trace that can help diagnose the cause of the exception. If you useerrorpage.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 thelog.directoryentry in thedefault.propertiesfile. The default value islogs. 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 Index |
|
Search
Feedback |