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 JavaBeans Component Design Conventions (page 270). 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.BookDBEJBHome
home interface,database.BookDBEJB
remote interface, and thedatabase.BookDBEJBImpl
implementation 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.
The source code for the application is located in the
j2eetutorial/examples/src/web/bookstore2
directory created when you unzip the tutorial bundle. To build, deploy, and run the example:- Go to
j2eetutorial/examples
and build the example by runningant bookstore2
. - Start the
j2ee
server. - 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
. - Add the
Bookstore2WAR
WAR to theBookstore2App
application. - Add the
BookDBEJB
enterprise bean to the application.- Select FileNew Enterprise Bean.
- Select Bookstore2App from the Create New JAR File In Application combo box.
- Type
BookDBJAR
in the JAR Display Name field. - Click Edit to add the content files.
- In the Edit Archive Contents dialog box, navigate to
j2eetutorial/examples/build/web/ejb
directory and add thedatabase
andexception
packages. Click Next. - Choose Session and Stateless for the Bean Type.
- Select
database.BookDBEJBImpl
for Enterprise Bean Class. - In the Remote Interfaces box, select
database.BookDBEJBHome
for Remote Home Interface anddatabase.BookDBEJB
for Remote Interface. - Enter
BookDBEJB
for Enterprise Bean Name. - Click Next and then click Finish.
- Add a resource reference for the Cloudscape database to the
BookDBEJB
bean. - Save
BookDBJAR
. - Add a reference to the enterprise bean
BookDBEJB
.- Select
Bookstore2WAR
. - Select the EJB Refs tab.
- Click Add.
- Enter
ejb/BookDBEJB
in the Coded Name column. - Select Session in the Type column.
- Select Remote in the Interfaces column.
- Enter
database.BookDBEJBHome
in the Home Interface column. - Enter
database.BookDBEJB
in the Local/Remote Interface column.
- Select
- Specify the JNDI Names.
- Select
Bookstore2App
. - In the Application table, locate the EJB component and enter
BookDBEJB
in the JNDI Name column. - In the References table, locate the EJB Ref and enter
BookDBEJB
in the JNDI Name column. - In the References table, locate the Resource component and enter
jdbc/Cloudscape
in the JNDI Name column.
- Select
- Enter the context root.
- Deploy the application.
- Open the bookstore URL
http://<
host
>:8000/bookstore2/enter
.
No comments:
Post a Comment