Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
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:
The data for the bookstore application is still maintained in a database. However, two changes are made to the database helper object
database.BookDB
:
- The database helper object is rewritten to conform to JavaBeans component design patterns as described in JavaBeans Component Design Conventions. This change is made so that JSP pages can access the helper object using JSP language elements specific to JavaBeans components.
- Instead of accessing the bookstore database directly, the helper object goes through a data access object
database.BookDBAO
.The implementation of the database helper object 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 separates 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 and business logic or operations that govern access and modification of this business data. The model notifies views when it changes and provides the ability for the view to query the model about its state. It also provides the ability for the controller to access application functionality encapsulated by the model. In the Duke's Bookstore application, the shopping cart and database helper 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 helper object.
- 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 and POST 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, the
Dispatcher
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 the previous chapter, 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) (see Chapter 17):
Custom tags are the preferred mechanism for performing a wide variety of dynamic processing tasks, including accessing databases, using enterprise services such as e-mail and directories, and flow control. In earlier versions of JSP technology, such tasks were performed with JavaBeans components in conjunction with scripting elements (discussed in Chapter 19). Though still available in JSP 2.0, scripting elements tend to make JSP pages more difficult to maintain because they mix presentation and logic, which is discouraged in page design. Custom tags are introduced in Using Custom Tags and described in detail in Chapter 18.
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
>/jwstutorial12/examples/web/bookstore2/
directory (see Building and Running the Examples). A samplebookstore2.war
is provided in<
INSTALL
>/jwstutorial12/examples/web/provided-wars/
. To build, package, deploy, and run the example:- Build and package the bookstore common files as described in Updating Web Applications.
- In a terminal window, go to
<
INSTALL
>/jwstutorial12/examples/web/bookstore2/
.- Run
Ant
build
. This target will spawn any necessary compilations and copy files to the<
INSTALL
>/jwstutorial12/examples/web/bookstore2/build/
directory.- Start Tomcat.
- Perform all the operations described in Updating Web Applications.
- Run
ant
install
. Theinstall
target notifies Tomcat that the new context is available.- Open the bookstore URL
http://localhost:8080/bookstore2/bookstore
. Click on the Start Shopping link and you will see the screen in Figure 16-2:
![]()
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 basename as a context parameter.
Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.