FAQ
History |
Previous Home Next |
Search
Feedback |
DividerThe 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:
Table 4-1 Duke's Bookstore Example JSP Pages Function JSP Pages Enter the bookstorebookstore.jsp
Create the bookstore bannerbanner.jsp
Browse the books offered for salecatalog.jsp
Put a book in a shopping cartcatalog.jsp
andbookdetails.jsp
Get detailed information on a specific bookbookdetails.jsp
Display the shopping cartshowcart.jsp
Remove one or more books from the shopping cartshowcart.jsp
Buy the books in the shopping cartcashier.jsp
Receive an acknowledgement for the purchasereceipt.jsp
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.BookDAO
.The implementation of the database helper object follows. The bean has two instance variables: the current book and a reference to the database enterprise bean.
public class BookDB { private String bookId = "0"; private BookDBEJB database = null; public BookDB () throws Exception { } public void setBookId(String bookId) { this.bookId = bookId; } public void setDatabase(BookDBEJB database) { this.database = database; } public BookDetails getBookDetails() throws Exception { try { return (BookDetails)database. getBookDetails(bookId); } catch (BookNotFoundException ex) { throw ex; } } ... }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 for the Duke's Bookstore application is located in the
<
INSTALL
>/j2eetutorial/examples/web/bookstore2
directory.To deploy and run the example:
- In the IDE, mount the filesystem
<
INSTALL
>/j2eetutorial/examples/web/bookstore2
.- Expand the
bookstore2
node.- Right-click the
DigitalClock
class and choose Compile.- Right-click the
WEB-INF
directory and choose Deploy.- Set up the PointBase database as described in Accessing Databases from Web Applications.
- Open the bookstore URL
http://localhost:80/bookstore2/enter
in a browser.To review the deployment settings:
- Expand the
WEB-INF
node.- Select the
web.xml
file.- Select the Deployment property sheet.
- Browse the listener property.
- Click the Listeners property and open the property editor.
- Note that the listener class is
listeners.ContextListener
.- Browse the JSP files definition and servlet mappings.
- Click the JSP Files property and open the property editor.
- Note the mappings between the JSP files and the URLs
/enter
/catalog
,/bookdetails
,/showcart
,/cashier
, and/receipt
.- Browse the resource references.
- Select the Resources Property sheet.
- Click the Resource References property and open the property editor.
- Note the resource reference named
jdbc/BookDB
.
FAQ
History |
Previous Home Next |
Search
Feedback |
All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.