The Example JSP Pages

This chapter describes the tasks involved in defining simple tags. We illustrate the tasks using excerpts from the JSP version of the Duke's Bookstore application discussed in The Example JSP Pages, rewritten here to take advantage of several custom tags:

The last section in the chapter, Examples, describes several tags in detail: a simple iteration tag and the set of tags in the tutorial-template tag library.

The tutorial-template tag library defines a set of tags for creating an application template. The template is a JSP page that has placeholders for the parts that need to change with each screen. Each of these placeholders is referred to as a parameter of the template. For example, a simple template might include a title parameter for the top of the generated screen and a body parameter to refer to a JSP page for the custom content of the screen. The template is created using a set of nested tags--definition, screen, and parameter--that are used to build a table of screen definitions for Duke's Bookstore. An insert tag to insert parameters from the table into the screen.

Figure 15-1 shows the flow of a request through the following Duke's Bookstore web components:

Request Flow Through Duke's Bookstore Components

Figure 15-1 Request Flow through Duke's Bookstore Components

The source code for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore3/ directory created when you unzip the tutorial bundle (see About the Examples). A sample bookstore3.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/bookstore3/.
  3. Run asant build. This target will spawn any necessary compilations and will copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore3/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 bookstore3. 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/bookstore3/bookstore3.war. The WAR Display Name field will show bookstore3.
    3. In the Context Root field, enter /bookstore3.
    4. Click Edit Contents.
    5. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore3/build/. Select the JSP pages bookstore.jsp, bookdetails.jsp, bookcatalog.jsp, bookshowcart.jsp, bookcashier.jsp, bookreceipt.jsp, and bookordererror.jsp, the tag files catalog.tag and shipDate.tag, the custom TLD, tutorial-template.tld, and the dispatcher, database, listeners, and template directories and click Add. Click OK.
    6. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore/dist/. Select bookstore.jar, and click Add. Click OK.
    7. Click Next.
    8. Select the Servlet radio button, then .
    9. Click Next.
    10. Select dispatcher.Dispatcher from the Servlet class combo box.
    11. Click Finish.
  4. Add the listener class listeners.ContextListener (described in Handling Servlet Life-Cycle Events).
    1. Select the Event Listeners tab.
    2. Click Add.
    3. Select the listeners.ContextListener class from drop-down field in the Event Listener Classes pane.
  5. Add the aliases.
    1. Select Dispatcher.
    2. Select the Aliases tab.
    3. Click Add and then type /bookstore in the Aliases field. Repeat to add the aliases /bookcatalog, /bookdetails, /bookshowcart, /bookcashier, /bookordererror, and /bookreceipt.
  6. 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.
  7. Set the prelude for all JSP pages.
    1. Select the JSP Properties tab.
    2. Click the Add button next to the Name list.
    3. Enter bookstore3.
    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.
  8. 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.
  9. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. Click OK.
    3. A pop-up dialog box will display the results of the deployment. Click Close.

To run the example, open the bookstore URL http://localhost:8080/bookstore3/bookstore.

See Troubleshooting for help with diagnosing common problems.