|
Home TOC Index |
|
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 in as a JSP page. Table 11-1 lists the functions and their corresponding JSP pages.
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 . 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 an enterprise bean. The advantage of using an enterprise bean is that the helper object is no longer responsible for connecting to the database; this job is taken over by the enterprise bean. Furthermore, because the EJB container maintains the pool of database connections, an enterprise bean can get a connection quicker than the helper object can. The relevant interfaces and classes for the enterprise bean are the
database.BookDBEJBHomehome interface,database.BookDBEJBremote interface, and thedatabase.BookDBEJBimplementation class, which contains all the JDBC calls to the database.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 code for the application is located in the
j2eetutorial/examples/src/web/bookstore2directory created when you unzip the tutorial bundle (see Downloading the Examples). To build, deploy, and run the example:
- Go to
j2eetutorial/examplesand build the example by runningant bookstore2.- Start the
j2eeserver.- Start
deploytool.- Start the Cloudscape database by executing
cloudscape -start.- If you have not already created the bookstore database, run
ant create-web-db.- Create a J2EE application called
Bookstore2App.
- Select File
New
Application.
- In the file chooser, navigate to
j2eetutorial/examples/src/web/bookstore2.- In the File Name field, enter
Bookstore2App.- Click New Application.
- Click OK.
- Add the
Bookstore2WARWAR to theBookstore2Appapplication.
- Select File
Add
Web WAR.
- In the Add Web WAR dialog box, navigate to
j2eetutorial/examples/build/web/bookstore2. Selectbookstore2.war. Click Add Web WAR.- Add the
BookDBEJBenterprise bean to the application.
- Select File
New Enterprise Bean.
- Select Bookstore2App from the Create New JAR File In Application combo box.
- Type
BookDBJARin the JAR Display Name field.- Click Edit to add the content files.
- In the Edit Archive Contents dialog box, navigate to
thej2eetutorial/examples/build/web/ejbdirectory and add thedatabaseandexceptionpackages. Click Next.- Chose Session and Stateless for the Bean Type.
- Select
database.BookDBEJBImplfor Enterprise Bean Class.- In the Remote Interfaces box, select
database.BookDBEJBHomefor Remote Home Interface anddatabase.BookDBEJBfor Remote Interface.- Enter
BookDBEJBfor Enterprise Bean Name.- Click Next and then click Finish.
- Add a resource reference for the Cloudscape database to the
BookDBEJBbean.
- Select the
BookDBEJBenterprise bean.- Select the Resource Refs tab.
- Click Add.
- Select
javax.sql.DataSourcefrom the Type column.- Enter
jdbc/BookDBin the Coded Name field.- Save
BookDBJAR.
- Select
BookDBJAR.- Select File
Save As.
- Navigate to the directory
examples/build/web/ejb.- Enter
bookDB.jarin the File Name field.- Click Save EJB JAR As.
- Add a reference to the enterprise bean
BookDBEJB.
- Select
Bookstore2WAR.- Select the EJB Refs tab.
- Click Add.
- Enter
ejb/BookDBEJBin the Coded Name column.- Select Session in the Type column.
- Select Remote in the Interfaces column.
- Enter
database.BookDBEJBHomein the Home Interface column.- Enter
database.BookDBEJBin the Local/Remote Interface column.- Specify the JNDI Names.
- Select
Bookstore2App.- In the Application table, locate the EJB component and enter
BookDBEJBin the JNDI Name column.- In the References table, locate the EJB Ref and enter
BookDBEJBin the JNDI Name column.- In the References table, locate the Resource component and enter
jdbc/Cloudscapein the JNDI Name column.- Enter the context root.
- Deploy the application.
- Open the bookstore URL
http://<host>:8000/bookstore2/enter.See Troubleshooting for help with diagnosing common problems.
|
Home TOC Index |
|
Search
Feedback |