The Example JSP Document

This chapter uses the Duke's Bookstore and books applications to demonstrate how to write JSP pages in XML syntax. The JSP pages of the bookstore5 application use the JSTL XML tags (see XML Tag Library) to manipulate the book data from an XML stream. The books application contains the JSP document books.jspx, which accesses the book data from the database and converts it into the XML stream. The bookstore5 application accesses this XML stream to get the book data.

These applications show how easy it is to generate XML data and stream it between web applications. The books application can be considered the application hosted by the book warehouse's server. The bookstore5 application can be considered the application hosted by the book retailer's server. In this way, the customer of the bookstore web site sees the list of books currently available, according to the warehouse's database.

The source for the Duke's Bookstore application is located in the <INSTALL>/j2eetutorial14/examples/web/bookstore5/ directory, which is created when you unzip the tutorial bundle (see About the Examples). Sample bookstore5.war and books.war files are provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/.

To build the Duke's Bookstore application, 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/bookstore5/.
  3. Start the Application Server.
  4. Perform all the operations described in Accessing Databases from Web Applications.

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

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

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

  1. Start deploytool.
  2. Create a web application called bookstore5 by running the New Web Application Wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  3. In the New Web Component wizard:
    1. In the WAR File screen, select the Create New Stand-Alone WAR Module radio button.
    2. In the War File field, enter <INSTALL>/j2eetutorial14/examples/web/bookstore5/bookstore5.war. The WAR Display Name field will show bookstore5.
    3. In the Context Root field, enter /bookstore5.
    4. Click Edit Contents.
    5. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore5/build/. Select everything in the build directory 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.
    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 each of the web components listed in Table 13-1. For each component:
    1. Select FileRight ArrowNewRight ArrowWeb Component.
    2. In the WAR File screen, click the Add to Existing WAR Module radio button. The WAR file contains all the JSP pages, so 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.
    8. From the tree, select the web component you added.
    9. Select the Aliases tab.
    10. Click Add. Enter the alias as shown in Table 13-1.
      Table 13-1 Duke's Bookstore Web Components  
      Web Component Name
      JSP Page
      Component Alias
      bookcashier
      bookcashier.jsp
      /bookcashier
      bookcatalog
      bookcatalog.jsp
      /bookcatalog
      bookdetails
      bookdetails.jsp
      /bookdetails
      bookreceipt
      bookreceipt.jsp
      /bookreceipt
      bookshowcart
      bookshowcart.jsp
      /bookshowcart
      bookstore
      bookstore.jsp
      /bookstore
  5. Add the context parameter that specifies the JSTL resource bundle base name.
    1. Select the bookstore5 WAR file from the tree.
    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 for the Value field.
  6. Add the context parameter that identifies the context path to the XML stream.
    1. On the Context tab, again click Add.
    2. Enter booksURL for the Coded Parameter.
    3. Enter http://localhost:8080/books/books.jspx in the Value field.
  7. 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 bookstore5.
    4. Click the Add URL button next to the URL Pattern list.
    5. Enter *.jsp.
    6. Click the Edit Preludes button next to the Include Preludes list.
    7. Click Add.
    8. Enter /template/prelude.jspf.
    9. Click OK.
    10. Click the Edit Codas button next to the Include Codas list.
    11. Click Add.
    12. Enter /template/coda.jspf.
    13. Click OK.
  8. Select FileRight ArrowSave.
  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 build the books application, follow these steps:

  1. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/books/.
  2. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/books/build/ directory.

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

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

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

  1. Create a web application called books by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  2. In the New Web Component wizard:
    1. In the WAR File screen, select the Create New Stand-Alone WAR Module radio button.
    2. Click Browse and in the file chooser, navigate to <INSTALL>/j2eetutorial14/examples/web/books/.
    3. In the File Name field, enter books.
    4. Click Create Module File. The WAR Display Name field will show books.
    5. In the Context Root field, enter /books.
    6. Click Edit Contents.
    7. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/books/build/. Select the JSP document books.jspx and the database and listeners directories and click Add.
    8. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and click Add. Click OK.
    9. Click Next.
    10. Select the JSP Page radio button.
    11. Click Next.
    12. Select /books.jspx from the JSP Filename combo box.
    13. Click Finish.
  3. Identify books.jspx as an XML document.
    1. Select the JSP Properties tab.
    2. Click the Add button next to the Name list.
    3. Enter books.
    4. Click the Add URL button next to the URL Pattern list.
    5. Enter *.jspx.
    6. Select the Is XML Document checkbox.
  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 the drop-down field in the Event Listener Classes pane.
  5. 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 for jdbc/BookDB frame.
  6. Select FileRight ArrowSave.
  7. Deploy the application.
    1. Select the books WAR file from the tree.
    2. Select ToolsRight ArrowDeploy.
    3. Click OK.
    4. A pop-up dialog box will display the results of the deployment. Click Close.

To run the applications, open the bookstore URL http://localhost:8080/bookstore5/bookstore.