The Example JSP Pages

This chapter illustrates JSTL using excerpts from the JSP version of the Duke's Bookstore application discussed in Chapter 12. Here, they are rewritten to replace the JavaBeans component database access object with direct calls to the database via the JSTL SQL tags. For most applications, it is better to encapsulate calls to a database in a bean. JSTL includes SQL tags for situations where a new application is being prototyped and the overhead of creating a bean may not be warranted.

The source for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore4/ directory created when you unzip the tutorial bundle (see About the Examples). A sample bookstore4.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build the example, follow these steps:

  1. Build and package the bookstore common files as described in Duke's Bookstore Examples.
  2. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/bookstore4/.
  3. Run asant build. This target will copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore4/build/ directory.
  4. Start the Application Server.
  5. Perform all the operations described in Accessing Databases from Web Applications.

To package and deploy the example using asant, follow these steps:

  1. Run asant create-bookstore-war.
  2. Run asant deploy-war.

To learn how to configure the example, use deploytool to package and deploy it:

  1. Start deploytool.
  2. Create a web application called bookstore4 by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  3. In the New Web Component wizard:
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. In the WAR File field, enter <INSTALL>/j2eetutorial14/examples/web/bookstore4/bookstore4.war. The WAR Display Name field will show bookstore4.
    3. In the Context Root field, enter /bookstore4.
    4. Click Edit Contents.
    5. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore4/build/. Select the JSP pages bookstore.jsp, bookdetails.jsp, bookcatalog.jsp, bookshowcart.jsp, bookcashier.jsp, and bookreceipt.jsp and the template directory and click Add.
    6. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore/dist/. Select bookstore.jar and click Add.
    7. Click OK.
    8. Click Next.
    9. Select the JSP Page radio button.
    10. Click Next.
    11. Select /bookstore.jsp from the JSP Filename combo box.
    12. Click Finish.
  4. Add the alias for the bookstore.jsp component:
    1. Select the bookstore web component.
    2. Select the Aliases tab.
    3. Click Add.
    4. Enter /bookstore into the Aliases table.
  5. Add each of the web components listed in Table 14-1. For each component:
    1. Select FileRight ArrowNewRight ArrowWeb Component.
    2. Click the Add to Existing WAR Module radio button. Because the WAR contains all the JSP pages, you do not have to add any more content.
    3. Click Next.
    4. Select the JSP Page radio button.
    5. Click Next.
    6. Select the page from the JSP Filename combo box.
    7. Click Finish.
      Table 14-1 Duke's Bookstore Web Components 
      Web Component Name
      JSP Page
      Alias
      bookcatalog
      bookcatalog.jsp
      /bookcatalog
      bookdetails
      bookdetails.jsp
      /bookdetails
      bookshowcart
      bookshowcart.jsp
      /bookshowcart
      bookcashier
      bookcashier.jsp
      /bookcashier
      bookreceipt
      bookreceipt.jsp
      /bookreceipt
  6. Set the alias for each web component.
    1. Select the component.
    2. Select the Aliases tab.
    3. Click the Add button.
    4. Enter the alias.
  7. Add the context parameter that specifies the JSTL resource bundle base name.
    1. Select the web module.
    2. Select the Context tab.
    3. Click Add.
    4. Enter javax.servlet.jsp.jstl.fmt.localizationContext in the Coded Parameter field.
    5. Enter messages.BookstoreMessages in the Value field.
  8. Set the prelude and coda for all JSP pages.
    1. Select the JSP Properties tab.
    2. Click the Add button next to the Name list.
    3. Enter bookstore4.
    4. Click the Add URL button.
    5. Enter *.jsp.
    6. Click the Edit Preludes button.
    7. Click Add.
    8. Enter /template/prelude.jspf.
    9. Click OK.
    10. Click the Edit Codas button.
    11. Click Add.
    12. Enter /template/coda.jspf.
    13. Click OK.
  9. Add a resource reference for the database.
    1. Select the Resource Ref's tab.
    2. Click Add.
    3. Enter jdbc/BookDB in the Coded Name field.
    4. Accept the default type javax.sql.DataSource.
    5. Accept the default authorization Container.
    6. Accept the default selected Shareable.
    7. Enter jdbc/BookDB in the JNDI name field of the Sun-specific Settings frame.
  10. Select FileRight ArrowSave.
  11. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. Click OK.

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

See Troubleshooting for help with diagnosing common problems.