Home TOC |
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 source for the application is located in examples/src/web/bookstore2. The data for the bookstore application is still maintained in a Cloudscape database. However, two changes are made to the database helper object database.BookDB:
- The helper object is rewritten to conform to JavaBeans component design patterns as described in JavaBeansTM Components in JSPTM Pages. 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. The relevant interfaces and classes for the enterprise bean are the database.BookDBEJBHome home interface, database.BookDBEJB remote interface, and the database.BookDBEJB implementation class, which contains all the JDBC calls to the database.
Finally, this version of the example uses 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.
To build, deploy, and run the example:
- Go to examples/src and build the example by running ant bookstore2.
- Start the Cloudscape database by executing cloudscape -start.
- If you have not already created the bookstore database, run ant create-web-db.
- Start the j2ee server.
- Start deploytool.
- Create a J2EE application called bookstore2.
- Select File->New Application or the New Application button.
- Enter bookstore2.ear in the Application File Name field.
- Click OK.
- Add the bookstore2 WAR to the bookstore2 application.
- Select File->Add to Application->Web WAR or the Web WAR button.
- In the Add Web WAR dialog, navigate to examples/build/web/bookstore2. Select bookstore2.war. Click Add Web WAR.
- Add the BookDBEJB enterprise bean to the application.
- Select File->New Enterprise Bean or the New Enterprise Bean button.
- Select bookstore2 from Enterprise Bean will Go In combo box.
- Type BookDBEJB in the JAR Display Name field.
- Click Add to add the content files. Navigate to the examples/build/web/ejb directory and add the database and exception packages. Click Next.
- Chose Session and Stateless for the Bean Type.
- Select BookDBEJBImpl for Enterprise Bean Class.
- Select BookDBEJBHome for Home Interface.
- Select BookDBEJB for Remote Interface.
- Enter BookDBEJB for Enterprise Bean Name.
- Type BookDBEJB in the JNDI Name field next to the BookDB component.
- Add a resource reference for the Cloudscape database.
- Select the BookDBEJB enterprise bean.
- Select the Resource Ref's tab.
- Click Add.
- Select javax.sql.DataSource from the Type column
- Enter jdbc/BookDB in the Coded Name field.
- Enter jdbc/Cloudscape in the JNDI Name field.
- Add a reference to the enterprise bean BookDBEJB.
- Select the bookstore2 WAR.
- Select the EJB Ref's tab.
- Click Add.
- Enter ejb/BookDBEJB in the Coded Name field.
- Enter Session in the Type field.
- Enter BookDBEJBHome in the Home field.
- Enter BookDBEJB in the Remote field.
- Enter BookDBEJB in the JNDI Name field.
- Deploy the application. Select Tools->Deploy Application or the Deploy Application Button. Click Next twice. Type BookDBEJB in the Application->JNDI Name field and bookstore2 for the context root. Click Next and Finish.
- Open the bookstore URL http://<host>:8000/bookstore2/enter.
See Troubleshooting for help with diagnosing common problems.
Home TOC |
Search
Feedback |