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 thebookstore5
application use the JSTL XML tags (see XML Tag Library) to manipulate the book data from an XML stream. Thebooks
application contains the JSP documentbooks.jspx
, which accesses the book data from the database and converts it into the XML stream. Thebookstore5
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. Thebookstore5
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). Samplebookstore5.war
andbooks.war
files are provided in<
INSTALL
>/j2eetutorial14/examples/web/provided-wars/
.To build the Duke's Bookstore application, follow these steps:
- Build and package the
bookstore
common files as described in Duke's Bookstore Examples.- In a terminal window, go to
<
INSTALL
>/j2eetutorial14/examples/web/bookstore5/
.- Start the Application Server.
- Perform all the operations described in Accessing Databases from Web Applications.
To package and deploy the application using
asant
, follow these steps:To learn how to configure the application, use
deploytool
to package and deploy it:
- Start
deploytool
.- Create a web application called
bookstore5
by running the New Web Application Wizard. Select FileNewWeb Component.- In the New Web Component wizard:
- In the WAR File screen, select the Create New Stand-Alone WAR Module radio button.
- In the War File field, enter
<
INSTALL
>/j2eetutorial14/examples/web/bookstore5/bookstore5.war
. The WAR Display Name field will showbookstore5
.- In the Context Root field, enter
/bookstore5
.- Click Edit Contents.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/j2eetutorial14/examples/web/bookstore5/build
/. Select everything in thebuild
directory and click Add. Click OK.- Add the shared bookstore library. Navigate to
<
INSTALL
>/j2eetutorial14/examples/web/bookstore/dist/
. Selectbookstore.jar
and Click Add.- Click OK.
- Click Next.
- Select the JSP Page radio button.
- Click Next.
- Select /
bookstore.jsp
from the JSP Filename combo box.- Click Finish.
- Add each of the web components listed in Table 13-1. For each component:
- Select FileNewWeb Component.
- 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.
- Click Next.
- Select the JSP Page radio button.
- Click Next.
- Select the page from the JSP Filename combo box.
- Click Finish.
- From the tree, select the web component you added.
- Select the Aliases tab.
- Click Add. Enter the alias as shown in Table 13-1.
- Add the context parameter that specifies the JSTL resource bundle base name.
- Add the context parameter that identifies the context path to the XML stream.
- Set the prelude and coda for all JSP pages.
- Select the JSP Properties tab.
- Click the Add button next to the Name list.
- Enter
bookstore5
.- Click the Add URL button next to the URL Pattern list.
- Enter
*.jsp
.- Click the Edit Preludes button next to the Include Preludes list.
- Click Add.
- Enter
/template/prelude.jspf
.- Click OK.
- Click the Edit Codas button next to the Include Codas list.
- Click Add.
- Enter
/template/coda.jspf
.- Click OK.
- Select FileSave.
- Deploy the application.
To build the
books
application, follow these steps:To package and deploy the application using
asant
, follow these steps:To learn how to configure the application, use
deploytool
to package and deploy it:
- Create a web application called
books
by running the New Web Component wizard. Select FileNewWeb Component.- In the New Web Component wizard:
- In the WAR File screen, select the Create New Stand-Alone WAR Module radio button.
- Click Browse and in the file chooser, navigate to
<
INSTALL
>/j2eetutorial14/examples/web/books/
.- In the File Name field, enter
books
.- Click Create Module File. The WAR Display Name field will show
books
.- In the Context Root field, enter
/books
.- Click Edit Contents.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/j2eetutorial14/examples/web/books/build
/. Select the JSP documentbooks.jspx
and thedatabase
andlisteners
directories and click Add.- Add the shared bookstore library. Navigate to
<
INSTALL
>/j2eetutorial14/examples/build/web/bookstore/dist/
. Selectbookstore.jar
and click Add. Click OK.- Click Next.
- Select the JSP Page radio button.
- Click Next.
- Select /
books.jspx
from the JSP Filename combo box.- Click Finish.
- Identify
books.jspx
as an XML document.- Add the listener class
listeners.ContextListener
(described in Handling Servlet Life-Cycle Events).- Add a resource reference for the database.
- Select the Resource Ref's tab.
- Click Add.
- Enter
jdbc/BookDB
in the Coded Name field.- Accept the default type
javax.sql.DataSource
.- Accept the default authorization
Container
.- Accept the default selected
Shareable
.- Enter
jdbc/BookDB
in the JNDI name field of the Sun-specific Settings for jdbc/BookDB frame.- Select FileSave.
- Deploy the application.
To run the applications, open the bookstore URL
http://localhost:8080/bookstore5/bookstore
.