The Example JSP Pages

To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in The Example Servlets (page 58) as a JSP page (see Table 4-1).

Table 4-1 Duke's Bookstore Example JSP Pages 
JSP Pages
Enter the bookstore.
Create the bookstore banner.
Browse the books offered for sale.
Add a book to the shopping cart.
bookcatalog.jsp and bookdetails.jsp
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.
Receive an acknowledgment for the purchase.

The data for the bookstore application is still maintained in a database and is accessed through com.sun.bookstore2.database.BookDBAO. However, the JSP pages access BookDBAO through the JavaBeans component com.sun.bookstore2.database.BookDB. This class allows the JSP pages to use JSP elements designed to work with JavaBeans components (see JavaBeans Component Design Conventions).

The implementation of the database bean follows. The bean has two instance variables: the current book and the data access object.

package database;
public class BookDB {
  private String bookId = "0";
  private BookDBAO database = null;

  public BookDB () throws Exception {
  public void setBookId(String bookId) {
    this.bookId = bookId;
  public void setDatabase(BookDAO database) {
    this.database = database;
  public Book getBook() 
    throws Exception {
    return (Book)database.getBook(bookId);

This version of the Duke's Bookstore application is organized along the Model-View-Controller (MVC) architecture. The MVC architecture is a widely used architectural approach for interactive applications that distributes functionality among application objects so as to minimize the degree of coupling between the objects. To achieve this, it divides applications into three layers: model, view, and controller. Each layer handles specific tasks and has responsibilities to the other layers:

Note: When employed in a web application, the MVC architecture is often referred to as a Model-2 architecture. The bookstore example discussed in Chapter 3, which intermixes presentation and business logic, follows what is known as a Model-1 architecture. The Model-2 architecture is the recommended approach to designing web applications.

In addition, this version of the application uses several custom tags from the JavaServer Pages Standard Tag Library (JSTL), described in Chapter 6:

Custom tags are the preferred mechanism for performing a wide variety of dynamic processing tasks, including accessing databases, using enterprise services such as email and directories, and implementing flow control. In earlier versions of JSP technology, such tasks were performed with JavaBeans components in conjunction with scripting elements (discussed in Chapter 8). Although still available in JSP 2.0 technology, scripting elements tend to make JSP pages more difficult to maintain because they mix presentation and logic, something that is discouraged in page design. Custom tags are introduced in Using Custom Tags and described in detail in Chapter 7.

Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See Including an Applet for a description of the JSP element that generates HTML for downloading the applet.

The source code for the application is located in the <INSTALL>/javaeetutorial5/examples/web/bookstore2/ directory (see Building the Examples (page xxxi)). To build and package the example, follow these steps:

  1. In a terminal window, go to <INSTALL>/javaeetutorial5/examples/web/bookstore2/.
  2. Run ant. This target will spawn any necessary compilations and will copy files to the <INSTALL>/javaeetutorial5/examples/web/bookstore2/build/ directory. It will then package the files into a WAR file and copy the WAR file to the <INSTALL>/javaeetutorial5/examples/web/bookstore2/dist/ directory.
  3. Start the Application Server.
  4. Perform all the operations described in Accessing Databases from Web Applications (page 54).

To deploy the example using ant, run ant deploy. The deploy target will output a URL for running the application. Ignore this URL.

To run the application, open the bookstore URL http://localhost:8080/bookstore2/books/bookstore. Click on the Start Shopping link and you will see the screen in Figure 4-2.

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

Figure 4-2 Book Catalog

See Troubleshooting (page 60) for help with diagnosing common problems related to the database server. If the messages in your pages appear as strings of the form ??? Key ???, the likely cause is that you have not provided the correct resource bundle base name as a context parameter.