Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

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, package, deploy, and run the applications, 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. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/bookstore5/build/ directory.
  4. Start the Application Server.
  5. Perform all the operations described in Accessing Databases from Web Applications.
  6. Start deploytool.
  7. Create a Web application called bookstore5 by running the New Web Application Wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  8. In the New Web Component wizard:
    1. In the Introduction screen, click Next.
    2. In the WAR File screen, select the Create New Stand-Alone WAR Module radio button.
    3. Click Browse and in the file chooser, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore5/.
    4. In the File Name field, enter bookstore5.
    5. Click Choose Module File.
    6. In the WAR Name field, enter bookstore5.
    7. In the Context Root field, enter /bookstore5.
    8. Click Edit Contents.
    9. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore5/build/. Select the JSP pages bookcashier.jsp, bookcatalog.jsp, bookdetails.jsp, bookreceipt.jsp, bookshowcart.jsp, bookstore.jsp, and parsebooks.jsp along with the template directory and click Add. Click OK.
    10. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and Click Add.
    11. Click OK.
    12. Click Next.
    13. Select the JSP file radio button.
    14. Click Next.
    15. Select /bookstore.jsp from the JSP Filename combo box.
    16. Click Finish.
  9. Add each of the Web components listed in Table 13-1. For each component:
    1. Select FileRight ArrowNewRight ArrowWeb Component.
    2. Click Next in the Introduction screen.
    3. 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.
    4. Click Next.
    5. Select the JSP Page radio button.
    6. Click Next.
    7. Select the page from the JSP Filename combo box.
    8. Click Finish.
    9. From the tree, select the Web component you added.
    10. Select the Aliases tab.
    11. 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
  10. Add the context parameter that specifies the JSTL resource bundle basename.
    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.
  11. 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.
  12. 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.
  13. Select FileRight ArrowSave.
  14. 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.
  15. Create a Web application called books by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  16. 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/bookstore5/.
    3. In the File Name field, enter books.
    4. Click Choose Module File.
    5. In the WAR Display Name field, enter books.
    6. In the Context Root field, enter /books.
    7. Click Edit Contents.
    8. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/bookstore5/build/. Select the JSP document books.jspx and the database, listeners, and template directories and click Add.
    9. Add the shared bookstore library. Navigate to <INSTALL>/j2eetutorial14/examples/build/web/bookstore/dist/. Select bookstore.jar and click Add. Click OK.
    10. Click Next.
    11. Select the JSP Page radio button.
    12. Click Next.
    13. Select /books.jspx from the JSP Filename combo box.
    14. Click Finish.
  17. 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.
  18. 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.
  19. 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.
  20. Select FileRight ArrowSave.
  21. 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.
  22. Open the bookstore URL http://localhost:8080/bookstore5/bookstore.
Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.