The Java EE 5 Tutorial

The Example Servlets

This chapter uses the Duke’s Bookstore application to illustrate the tasks involved in programming servlets. The source code for the bookstore application is located in the tut-install/javaeetutorial5/examples/web/bookstore1/ directory, which is created when you unzip the tutorial bundle (see Building the Examples).

Table 4–1 lists the servlets that handle each bookstore function. You can find these servlet classes in tut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/. Each programming task is illustrated by one or more servlets. For example, BookDetailsServlet illustrates how to handle HTTP GET requests, BookDetailsServlet and CatalogServlet show how to construct responses, and CatalogServlet illustrates how to track session information.

Table 4–1 Duke’s Bookstore Example Servlets



Enter the bookstore 


Create the bookstore banner 


Browse the bookstore catalog 


Put a book in a shopping cart 



Get detailed information on a specific book 


Display the shopping cart 


Remove one or more books from the shopping cart 


Buy the books in the shopping cart 


Send an acknowledgment of the purchase 


The data for the bookstore application is maintained in a database and accessed through the database access class database.BookDBAO. The database package also contains the class Book which represents a book. The shopping cart and shopping cart items are represented by the classes cart.ShoppingCart and cart.ShoppingCartItem, respectively.

    To deploy and run the application using NetBeans IDE, follow these steps:

  1. Perform all the operations described in Accessing Databases from Web Applications.

  2. In NetBeans IDE, select File->Open Project.

  3. In the Open Project dialog, navigate to:

  4. Select the bookstore1 folder.

  5. Select the Open as Main Project check box and the Open Required Projects check box.

  6. Click Open Project.

  7. In the Projects tab, right-click the bookstore1 project, and select Undeploy and Deploy.

  8. To run the application, open the bookstore URL http://localhost:8080/bookstore1/bookstore.

    To deploy and run the application using Ant, follow these steps:

  1. In a terminal window, go to tut-install/javaeetutorial5/examples/web/bookstore1/.

  2. Type ant. This command will spawn any necessary compilations, copy files to the tut-install/javaeetutorial5/examples/web/bookstore1/build/ directory, and create a WAR file and copy it to the tut-install/javaeetutorial5/examples/web/bookstore1/dist/ directory.

  3. Start the Application Server.

  4. Perform all the operations described in Creating a Data Source in the Application Server.

  5. To deploy the example, type ant deploy. The deploy target outputs a URL for running the application. Ignore this URL, and instead use the one shown in the next step.

  6. To run the application, open the bookstore URL http://localhost:8080/bookstore1/bookstore.

To learn how to configure the example, refer to the deployment descriptor (the web.xml file), which includes the following configurations:

Troubleshooting Duke's Bookstore Database Problems

The Duke’s Bookstore database access object returns the following exceptions:

Because you 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 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.