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.BookDBEJBHomehome interface,database.BookDBEJBremote interface, and thedatabase.BookDBEJBImplimplementation 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/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. - Add the
Bookstore2WARWAR to theBookstore2Appapplication. - 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. - Choose 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.
- Select File
- Add a resource reference for the Cloudscape database to the
BookDBEJBbean. - Save
BookDBJAR. - 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.
- Select
- 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.
- Select
- Enter the context root.
- Deploy the application.
- Open the bookstore URL
http://<host>:8000/bookstore2/enter.
No comments:
Post a Comment