The Example Servlets
This chapter uses the Duke's Bookstore application to illustrate the tasks involved in programming servlets. Table 3-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 database access class
database.BookDBAO. Thedatabasepackage also contains the classBookwhich 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
<INSTALL>/javaeetutorial5javaeetutorial5/examples/web/bookstore1/directory, which is created when you unzip the tutorial bundle (see Building the Examples, page xxxi). To build the application, follow these steps:
- In a terminal window, go to
<INSTALL>/javaeetutorial5/examples/web/bookstore1/.- Run
ant. This target will spawn any necessary compilations, copy files to the<INSTALL>/javaeetutorial5/examples/web/bookstore1/build/directory, and create a WAR file and copy it to the<INSTALL>/javaeetutorial5/examples/web/bookstore1/dist/directory.- Start the Application Server.
- Perform all the operations described in Accessing Databases from Web Applications (page 54).
To deploy the example, run
ant deploy. The deploy target outputs a URL for running the application. Ignore this URL, and instead use the one shown at the end of this section.To learn how to configure the example, refer to the deployment descriptor (the
web.xmlfile), which includes the following configurations:
- A
display-nameelement that specifies the name that tools use to identify the application.- A
context-paramelement that specifies the JSTL resource bundle base name.- A set of
filterelements that identify servlet filters contained in the application.- A set of
filter-mappingelements that identify which servlets will have their requests or responses filtered by the filters identified by thefilterelements. Afilter-mappingelement can define more than one servlet mapping and more than one URL pattern for a particular filter.- A
listenerelement that identifies theContextListenerclass used to create and remove the database access.- A set of
servletelements that identify all the servlet instances of the application.- A set of
servlet-mappingelements that map the servlets to URL patterns. More than one URL pattern can be defined for a particular servlet.- A set of error-page mappings that map exception types to an HTML page, so that the HTML page opens when an exception of that type is thrown by the application.
- A
resource-refelement that declares the resource reference for the database used in Duke's Bookstore.To run the application, open the bookstore URL
http://localhost:8080/bookstore1/bookstore.Troubleshooting
The Duke's Bookstore database access object 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 or the server has not been started or has crashed. You can populate the database by runningantcreate-tables.BooksNotFoundException: Returned if the bookstore data can't be retrieved. This will occur if you haven't loaded the bookstore database with data 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 the database server hasn't been started.Because we have specified an error page, you will see the message
If you don't specify an error page, the web container generates a default page containing the message
and a stack trace that can help you diagnose the cause of the exception. If you use
errorpage.html, you will have to look in the server log to determine the cause of the exception.