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:
- Run
asant
create-bookstore-war
.- Run
asant
deploy-war
.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 FileRight ArrowNewRight ArrowWeb 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 FileRight ArrowNewRight ArrowWeb 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.
Table 13-1 Duke's Bookstore Web Components Web Component Name JSP Page Component Aliasbookcashier
bookcashier.jsp
/bookcashier
bookcatalog
bookcatalog.jsp
/bookcatalog
bookdetails
bookdetails.jsp
/bookdetails
bookreceipt
bookreceipt.jsp
/bookreceipt
bookshowcart
bookshowcart.jsp
/bookshowcart
bookstore
bookstore.jsp
/bookstore
- Add the context parameter that specifies the JSTL resource bundle base name.
- Select the
bookstore5
WAR file from the tree.- Select the Context tab.
- Click Add.
- Enter
javax.servlet.jsp.jstl.fmt.localizationContext
in the Coded Parameter field.- Enter
messages.BookstoreMessages
for the Value field.- Add the context parameter that identifies the context path to the XML stream.
- On the Context tab, again click Add.
- Enter
booksURL
for the Coded Parameter.- Enter
http://localhost:8080/books/books.jspx
in the Value field.- 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 FileRight ArrowSave.
- Deploy the application.
- Select ToolsRight ArrowDeploy.
- Click OK.
- A pop-up dialog box will display the results of the deployment. Click Close.
To build the
books
application, follow these steps:
- In a terminal window, go to
<
INSTALL
>/j2eetutorial14/examples/web/books/
.- 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:
- Run
asant
create-bookstore-war
.- Run
asant
deploy-war
.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 FileRight ArrowNewRight ArrowWeb 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.
- Select the JSP Properties tab.
- Click the Add button next to the Name list.
- Enter
books
.- Click the Add URL button next to the URL Pattern list.
- Enter
*.jspx
.- Select the Is XML Document checkbox.
- Add the listener class
listeners.ContextListener
(described in Handling Servlet Life-Cycle Events).
- Select the Event Listeners tab.
- Click Add.
- Select the
listeners.ContextListener
class from the drop-down field in the Event Listener Classes pane.- 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 FileRight ArrowSave.
- Deploy the application.
- Select the
books
WAR file from the tree.- Select ToolsRight ArrowDeploy.
- Click OK.
- 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
.