Home TOC |
![]() ![]() ![]() |
The Example Servlets
This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 13-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 HTTPGET
requests,BookDetailsServlet
andCatalogServlet
show how to construct responses, andCatalogServlet
illustrates how to track session information.The data for the bookstore application is maintained in a database and accessed through the helper class
database.BookDB
. Thedatabase
package also contains the classBookDetails
, which represents a book. The shopping cart and shopping cart items are represented by the classescart.ShoppingCart
andcart.ShoppingCartItem
, respectively.The source code for the bookstore application is located in the
docs/tutorial/examples/web/bookstore1
directory created when you unzip the tutorial bundle (see Running the Examples). To build, deploy, and run the example:
- In a terminal window, go to
docs/tutorial/examples/web/bookstore1.
- Run
ant build
. Thebuild
target will spawn any necessary compilations and copy files to thedocs/tutorial/examples/web/bookstore1/build
directory.- Make sure Tomcat is started.
- Run
ant
install
. Theinstall
target 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).
- Open the bookstore URL
http://localhost:8080/bookstore1/enter
.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 runningant
create-web-db
or 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 runningant
create-web-db
or 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.jar
to<
JWSDP_HOME
>/common/lib
or if the Pointbase server hasn't been started.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 Occurred
and 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
>/logs
and are named jwsdp_log.<
date
>.txt
.
Home TOC |
![]() ![]() ![]() |