The J2EETM Tutorial

The Example JSP Pages

This chapter describes the tasks involved in using and defining tags. The chapter illustrates the tasks with excerpts from the JSP version of the Duke's Bookstore application discussed in Chapter 11 rewritten to take advantage of two tag libraries: Struts and tutorial-template. The section in this chapter entitled Examples, describes some tags in detail: the iterate tag from Struts and the set of tags in the tutorial-template tag library.

The Struts tag library provides a framework for building internationalized Web applications that implement the Model-View-Controller design pattern. Struts includes a comprehensive set of utility custom tags for handling

The Duke's Bookstore application uses tags from the Struts bean and logic sublibraries.

The tutorial-template tag library defines a set of tags for creating an application template. The template is a JSP page with 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 could 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 with a set of nested tags--definition, screen, and parameter--that are used to build a table of screen definitions for Duke's Bookstore and with an insert tag to insert parameters from the table into the screen.

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

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

The source for the Duke's Bookstore application is located in the j2eetutorial/examples/src/web/bookstore3 directory created when you unzip the tutorial bundle (see Downloading the Examples). To build, deploy, and run the example:

  1. Go to j2eetutorial/examples and build the application by executing ant bookstore3 (see How to Build and Run the Examples).
  2. Download and unpack Struts version 1.0 from
    Copy struts-bean.tld, struts-logic.tld, and struts.jar from jakarta-struts-1.0/lib to examples/build/web/bookstore3
  3. Start the j2ee server.
  4. Start deploytool.
  5. Start the Cloudscape database by executing cloudscape -start.
  6. If you have not already created the bookstore database, run ant create-web-db.
  7. Create a J2EE application called Bookstore3App.
    1. Select FileNew Application.
    2. In the file chooser, navigate to j2eetutorial/examples/src/web/bookstore3.
    3. In the File Name field, enter Bookstore3App.
    4. Click New Application.
    5. Click OK.
  8. Create the WAR and add the DispatcherServlet Web component and all of the Duke's Bookstore content to Bookstore3App.
    1. Select FileNewWeb Component.
    2. Click the Create New WAR File in Application radio button and select Bookstore3App from the combo box. Enter Bookstore3WAR in the field labeled WAR Display Name.
    3. Click Edit to add the content files. In the Edit Contents dialog box, navigate to j2eetutorial/examples/build/web/bookstore3. Select Dispatcher.class and click Add. Add the JSP pages banner.jsp, bookstore.jsp, bookdetails.jsp, catalog.jsp, showcart.jsp, cashier.jsp, receipt.jsp, initdestroy.jsp, template.jsp, screendefinitions.jsp, and errorpage.jsp. Add duke.books.gif, struts-bean.tld, struts-logic.tld, tutorial-template.tld, and struts.jar. Add the cart, database, messages, taglib, and util packages. Click OK.
    4. Click Next.
    5. Select the Servlet radio button.
    6. Click Next.
    7. Select Dispatcher from the Servlet Class combo box.
    8. Click Next twice.
    9. In the Component Aliases pane, click Add and then type /enter in the Alias field. Repeat to add the aliases /catalog, /bookdetails, /showcart, /cashier, and /receipt.
    10. Click Finish.
  9. Add the BookDBEJB enterprise bean that you created in the section The Example JSP Pages.
    1. Select FileAddEJB JAR.
    2. Navigate to the directory examples/build/web/ejb.
    3. Select bookDB.jar.
    4. Click Add EJB JAR.
  10. Add a reference to the enterprise bean BookDBEJB.
    1. Select Bookstore3WAR.
    2. Select the EJB Refs tab.
    3. Click Add.
    4. Enter ejb/BookDBEJB in the Coded Name column.
    5. Enter Session in the Type column.
    6. Select Remote in the Interfaces column.
    7. Enter database.BookDBEJBHome in the Home Interface column.
    8. Enter database.BookDBEJB in the Local/Remote Interface column.
  11. Add the tag library URI to location mappings (see Declaring Tag Libraries):
    1. Select the File Refs tab
    2. Click the Add button in the JSP Tag Libraries subpane.
    3. Enter the relative URI /tutorial-template in the Coded Reference field.
    4. Enter the absolute location /WEB-INF/tutorial-template.tld in the Tag Library field.
    5. Repeat for /struts-bean to /WEB-INF/struts-bean.tld and /struts-logic to /WEB-INF/struts-logic.tld.
  12. Specify the JNDI names.
    1. Select Bookstore3App.
    2. In the Application table, locate the EJB component and enter BookDBEJB in the JNDI Name column.
    3. In the References table, locate the EJB Ref and enter BookDBEJB in the JNDI Name column.
    4. In the References table, locate the Resource component and enter jdbc/Cloudscape in the JNDI Name column.
  13. Enter the context root.
    1. Select the Web Context tab.
    2. Enter bookstore3.
  14. Deploy the application.
    1. Select ToolsDeploy.
    2. Click Finish.
  15. Open the bookstore URL http://<host>:8000/bookstore3/enter.

See Troubleshooting for help with diagnosing common problems.