The Example JSP Pages
To illustrate JSP technology, this chapter rewrites each servlet in the Duke's Bookstore application introduced in The Example Servlets as a JSP page (see Table 12-1).
Table 12-1 Duke's Bookstore Example JSP Pages Function JSP Pages Enter the bookstore.bookstore.jsp
Create the bookstore banner.banner.jsp
Browse the books offered for sale.bookcatalog.jsp
Add a book to the shopping cart.bookcatalog.jsp
andbookdetails.jsp
Get detailed information on a specific book.bookdetails.jsp
Display the shopping cart.bookshowcart.jsp
Remove one or more books from the shopping cart.bookshowcart.jsp
Buy the books in the shopping cart.bookcashier.jsp
Receive an acknowledgment for the purchase.bookreceipt.jsp
The data for the bookstore application is still maintained in a database and is accessed through
database.BookDBAO
. However, the JSP pages accessBookDBAO
through the JavaBeans componentdatabase.BookDB
. This class allows the JSP pages to use JSP elements designed to work with JavaBeans components (see JavaBeans Component Design Conventions).The implementation of the database bean follows. The bean has two instance variables: the current book and the data access object.
package database; public class BookDB { private String bookId = "0"; private BookDBAO database = null; public BookDB () throws Exception { } public void setBookId(String bookId) { this.bookId = bookId; } public void setDatabase(BookDAO database) { this.database = database; } public BookDetails getBookDetails() throws Exception { return (BookDetails)database.getBookDetails(bookId); } ... }This version of the Duke's Bookstore application is organized along the Model-View-Controller (MVC) architecture. The MVC architecture is a widely used architectural approach for interactive applications that distributes functionality among application objects so as to minimize the degree of coupling between the objects. To achieve this, it divides applications into three layers: model, view, and controller. Each layer handles specific tasks and has responsibilities to the other layers:
- The model represents business data, along with business logic or operations that govern access and modification of this business data. The model notifies views when it changes and lets the view query the model about its state. It also lets the controller access application functionality encapsulated by the model. In the Duke's Bookstore application, the shopping cart and database access object contain the business logic for the application.
- The view renders the contents of a model. It gets data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller. The Duke's Bookstore JSP pages format the data stored in the session-scoped shopping cart and the page-scoped database bean.
- The controller defines application behavior. It dispatches user requests and selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a web application, user inputs are HTTP
GET
andPOST
requests. A controller selects the next view to display based on the user interactions and the outcome of the model operations. In the Duke's Bookstore application, theDispatcher
servlet is the controller. It examines the request URL, creates and initializes a session-scoped JavaBeans component--the shopping cart--and dispatches requests to view JSP pages.
Note: When employed in a web application, the MVC architecture is often referred to as a Model-2 architecture. The bookstore example discussed in Chapter 11, which intermixes presentation and business logic, follows what is known as a Model-1 architecture. The Model-2 architecture is the recommended approach to designing web applications.
In addition, this version of the application uses several custom tags from the JavaServer Pages Standard Tag Library (JSTL), described in Chapter 14 :
c:if
,c:choose
,c:when
, andc:otherwise
for flow controlc:set
for setting scoped variablesc:url
for encoding URLsfmt:message
,fmt:formatNumber
, andfmt:formatDate
for providing locale-sensitive messages, numbers, and datesCustom tags are the preferred mechanism for performing a wide variety of dynamic processing tasks, including accessing databases, using enterprise services such as email and directories, and implementing flow control. In earlier versions of JSP technology, such tasks were performed with JavaBeans components in conjunction with scripting elements (discussed in Chapter 16). Although still available in JSP 2.0 technology, scripting elements tend to make JSP pages more difficult to maintain because they mix presentation and logic, something that is discouraged in page design. Custom tags are introduced in Using Custom Tags and described in detail in Chapter 15.
Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See Including an Applet for a description of the JSP element that generates HTML for downloading the applet.
The source code for the application is located in the
<
INSTALL
>/j2eetutorial14/examples/web/bookstore2/
directory (see Building the Examples). A samplebookstore2.war
is provided in<
INSTALL
>/j2eetutorial14/examples/web/provided-wars/
. To build the example, 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/bookstore2/
.- Run
asant
build
. This target will spawn any necessary compilations and will copy files to the<
INSTALL
>/j2eetutorial14/examples/web/bookstore2/build/
directory.- Start the Application Server.
- Perform all the operations described in Accessing Databases from Web Applications.
To package and deploy the example using
asant
, follow these steps:
- Run
asant
create-bookstore-war
.- Run
asant
deploy-war
.To learn how to configure the example, use
deploytool
to package and deploy it:
- Start
deploytool
.- Create a web application called
bookstore2
by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.- In the New Web Component wizard:
- Select the Create New Stand-Alone WAR Module radio button.
- Click Browse.
- In the WAR File field, enter
<
INSTALL
>/j2eetutorial14/examples/web/bookstore2/bookstore2.war
. The WAR Display Name field will showbookstore2
.- In the Context Root field, enter
/bookstore2
.- Click Edit Contents.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/j2eetutorial14/examples/web/bookstore2/build/
. Select the JSP pagesbookstore.jsp
,bookdetails.jsp
,bookcatalog.jsp
,bookshowcart.jsp
,bookcashier.jsp
,bookordererror.jsp
,bookreceipt.jsp
,duke.books.gif
,and theclock
,dispatcher
,database
,listeners
, andtemplate
directories and click Add.- Move
/WEB-INF/classes/clock/
to the root directory of the WAR. By default,deploytool
packages all classes in/WEB-INF/classes/
. Becauseclock/DigitalClock.class
is a client-side class, it must be packaged in the root directory. To do this, simply drag theclock
directory from/WEB-INF/classes/
to the root directory in the pane labeled Contents ofbookstore2
.- Add the shared bookstore library. Navigate to
<
INSTALL
>/j2eetutorial14/examples/web/bookstore/dist/
. Selectbookstore.jar
, and click Add. Click OK, then click Next.- Select the Servlet radio button, then click Next.
- Select
dispatcher.Dispatcher
from the Servlet class combo box, then click Finish.- 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 drop-down field in the Event Listener Classes pane.- Add the aliases.
- Select the
Dispatcher
web component.- Select the Aliases tab.
- Click Add and then type
/bookstore
in theAliases
field. Repeat to add the aliases/bookcatalog
,/bookdetails
,/bookshowcart
,/bookcashier
,/bookordererror,
and/bookreceipt
.- Add the context parameter that specifies the JSTL resource bundle base name.
- Select the web module.
- Select the Context tab.
- Click Add.
- Enter
javax.servlet.jsp.jstl.fmt.localizationContext
in the Coded Parameter field.- Enter
messages.BookstoreMessages
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
bookstore2
.- Click the Add URL button.
- Enter
*.jsp
.- Click the Edit Preludes button.
- Click Add.
- Enter
/template/prelude.jspf
.- Click OK.
- Click the Edit Codas button.
- Click Add.
- Enter
/template/coda.jspf
.- Click OK.
- 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 frame.- Select FileRight ArrowSave.
- Deploy the application.
- Select ToolsRight ArrowDeploy.
- Click OK.
To run the application, open the bookstore URL
http://localhost:8080/bookstore2/bookstore
. Click on the Start Shopping link and you will see the screen in Figure 12-2.Figure 12-2 Book Catalog
See Troubleshooting for help with diagnosing common problems related to the database server. If the messages in your pages appear as strings of the form
???
Key
???
, the likely cause is that you have not provided the correct resource bundle base name as a context parameter.