|
Home
TOC Index |
|
The Example Servlets
This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 12-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
<JWSDP_HOME>/docs/tutorial/examples/web/bookstore1directory created when you unzip the tutorial bundle (see Running the Examples).To build, install, and run the example:
- In a terminal window, go to
<JWSDP_HOME>/docs/tutorial/examples/web/bookstore1.- Run
ant build. Thebuildtarget will spawn any necessary compilations and copy files to the<JWSDP_HOME>/docs/tutorial/examples/web/bookstore1/builddirectory.- Make sure Tomcat is started.
- Run
antinstall. Theinstalltarget notifies Tomcat that the new context is available.- Start the PointBase database server and populate the database if you have not done so already (see Accessing Databases from Web Applications).
- To run the application, open the bookstore URL
http://localhost:8080/bookstore1/enter.To use the
Antdeploytask to deploy the application:
- Run
antpackage. The package task creates a WAR file containing the application classes inWEB-INF/classesand thecontext.xmlfile inMETA-INF.- Make sure Tomcat is started.
- Run
antdeploy. Thedeploytarget copies the WAR to Tomcat and notifies Tomcat that the new context is available.To use
deploytoolto deploy the application:
- Make sure Tomcat is started.
- Start
deploytool.- Create a Web application called
bookstore1.
- Select File
New Web Application.
- Click Browse.
- In the file chooser, navigate to
<JWSDP_HOME>/docs/tutorial/examples/web/bookstore1/build.- In the File Name field, enter
bookstore1.- Click Choose Module File.
- In the WAR Display Name field, enter
bookstore1.- In the Edit Archive Contents dialog box, navigate to
<JWSDP_HOME>/docs/tutorial/examples/web/bookstore1/build. Selecterrorpage.htmlandduke.books.gifand click Add.Navigate toWEB-INF/classes, and selectBannerServlet.class,BookStoreServlet.class,BookDetailsServlet.class,CatalogServlet.class,ShowCartServlet.class,CashierServlet.class, andReceiptServlet.class. and thecart,database,exception,filters,listeners,messages, andutilpackages. Click Add, then click OK.- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select
BannerServletfrom the Servlet Class combo box.- Click Finish.
- Add each of the Web components listed in Table 12-2. For each servlet:
- Select File
Edit Web Application.
- Click the Add to Existing WAR Module radio button Since the WAR contains all of the servlet classes, you do not have to add any more content.
- Click Next.
- Select the Servlet radio button.
- Click Next.
- Select the servlet from the Servlet Class combo box.
- Click Finish.
- Add aliases for each of the components.
- Select the component.
- Select the Aliases tab.
- Click Add and then type
the alias in the Aliases 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 drop down field in the Event Listener Classes panel.- Add an error page (described in Handling Errors.
- Select the File Refs tab.
- Click Add in the Error Mapping panel.
- 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. The deploytool will automatically enterHitCounterFilterin the Display Name column.- Click Add.
- Select
filters.OrderFilterfrom the Filter Class column. The deploytool 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.- Add a resource reference for the database.
- Select the
WAR.- Select the Resource Refs tab.
- Click Add.
- Select
javax.sql.DataSourcefrom the Type column- Enter
jdbc/BookDBin the Coded Name field.- Click the Import Data Sources button.
- Dismiss the confirmation dialog.
- Select
pointbasefrom the drop down list.- Deploy the application.
Troubleshooting
Common Problems and Their Solutions 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 runningantcreate-book-dbor if the database 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 runningantcreate-book-dbor if the database server hasn't been started or it has crashed.UnavailableException--Returned 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>/lib/pbclient42.jarto<JWSDP_HOME>/common/lib, if the PointBase server hasn't been started, or if you have not defined a data source in Tomcat that references the PointBase database (see Defining a Data Source in Tomcat).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 messageA Servlet Exception Has Occurredand a stack trace that can help diagnose the cause of the exception. If you use theerrorpage.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.
|
Home
TOC Index |
|
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.