The Example JSP Pages



The Example JSP Pages

To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in Java Servlet Technology (page 367) as a JSP page.

Duke's Bookstore Example JSP Pages

Function

JSP Pages

Enter the bookstore

bookstore.jsp

Create the bookstore banner

banner.jsp

Browse the books offered for sale

catalog.jsp

Put a book in a shopping cart

catalog.jsp and bookdetails.jsp

Get detailed information on a specific book

bookdetails.jsp

Display the shopping cart

showcart.jsp

Remove one or more books from the shopping cart

showcart.jsp

Receive an acknowledgement for the purchase

receipt.jsp

Buy the books in the shopping cart

cashier.jsp

The data for the bookstore application is still maintained in a database. However, two changes are made to the database helper object database.BookDB:

  • The database helper object is rewritten to conform to JavaBeans component design patterns as described in JavaBeans Component Design Conventions (page 424). This change is made so that JSP pages can access the helper object using JSP language elements specific to JavaBeans components.

  • Instead of accessing the bookstore database directly, the helper object goes through a data access object database.BookDAO.

The implementation of the database helper object follows. The bean has two instance variables: the current book and a reference to the database enterprise bean.


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

    public BookDB () throws Exception {
    } 
    public void setBookId(String bookId) {
        this.bookId = bookId; 
    } 
    public void setDatabase(BookDBEJB database) {
        this.database = database; 
    } 
    public BookDetails getBookDetails() 
        throws Exception {
        try {
            return (BookDetails)database. 
                    getBookDetails(bookId); 
        } catch (BookNotFoundException ex) {
            throw ex; 
        } 
    } 
    ... 
}

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

The source for the application is located in the docs/tutorial/examples/web/bookstore2 directory created when you unzip the tutorial bundle (see Running the Examples (page xx)). To build, deploy, and run the example:

  1. Go to the bookstore2 directory and build and deploy the example by running ant. This runs the default ant target deploy that depends on the build target. The build target will spawn any necessary compilations and copy files to the docs/tutorial/examples/web/bookstore2/build directory. The deploytarget copies the bookstore2 context file to <JWSDP_HOME >/webappsas described in Running Web Applications (page 360).

  2. Start the Pointbase database server (see Accessing Databases from Web Applications (page 363)).

  3. Start or restart Tomcat.

  4. Open the bookstore URL http://localhost:8080/bookstore2/enter.

See Common Problems and Their Solutions (page 65) and Troubleshooting (page 369) for help with diagnosing common problems.