FAQ
History
PreviousHomeNext Search
Feedback
Divider

The Example JSP Pages

To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in The Example Servlets as a JSP page:

Table 4-1 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
Buy the books in the shopping cart
cashier.jsp
Receive an acknowledgement for the purchase
receipt.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 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 for a description of the JSP element that generates HTML for downloading the applet.

The source for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial/examples/web/bookstore2 directory.

To deploy and run the example:

  1. In the IDE, mount the filesystem
    <INSTALL>/j2eetutorial/examples/web/bookstore2.
  2. Expand the bookstore2 node.
  3. Right-click the DigitalClock class and choose Compile.
  4. Right-click the WEB-INF directory and choose Deploy.
  5. Set up the PointBase database as described in Accessing Databases from Web Applications.
  6. Open the bookstore URL http://localhost:80/bookstore2/enter in a browser.

To review the deployment settings:

  1. Expand the WEB-INF node.
  2. Select the web.xml file.
  3. Select the Deployment property sheet.
  4. Browse the listener property.
    1. Click the Listeners property and open the property editor.
    2. Note that the listener class is listeners.ContextListener.
  5. Browse the JSP files definition and servlet mappings.
    1. Click the JSP Files property and open the property editor.
    2. Note the mappings between the JSP files and the URLs /enter /catalog, /bookdetails, /showcart, /cashier, and /receipt.
  6. Browse the resource references.
    1. Select the Resources Property sheet.
    2. Click the Resource References property and open the property editor.
    3. Note the resource reference named jdbc/BookDB.
Divider
FAQ
History
PreviousHomeNext Search
Feedback
Divider

All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.