Sunday 18 December 2011

The JSP Pages Examples

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.

Table 11-1 Duke's Bookstore Example JSP Pages 
FunctionJSP 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 and bookdetails.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 (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 the database.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:
  1. Go to j2eetutorial/examples and build the example by running ant bookstore2.
  2. Start the j2ee server.
  3. Start deploytool.
  4. Start the Cloudscape database by executing cloudscape -start.
  5. If you have not already created the bookstore database, run ant create-web-db.
  6. Create a J2EE application called Bookstore2App.
    1. Select FileNewApplication.
    2. In the file chooser, navigate to j2eetutorial/examples/src/web/bookstore2.
    3. In the File Name field, enter Bookstore2App.
    4. Click New Application.
    5. Click OK.
  7. Add the Bookstore2WAR WAR to the Bookstore2App application.
    1. Select FileAddWeb WAR.
    2. In the Add Web WAR dialog box, navigate to j2eetutorial/examples/build/web/bookstore2. Select bookstore2.war. Click Add Web WAR.
  8. Add the BookDBEJB enterprise bean to the application.
    1. Select FileNew Enterprise Bean.
    2. Select Bookstore2App from the Create New JAR File In Application combo box.
    3. Type BookDBJAR in the JAR Display Name field.
    4. Click Edit to add the content files.
    5. In the Edit Archive Contents dialog box, navigate to the j2eetutorial/examples/build/web/ejb directory and add the database and exception packages. Click Next.
    6. Choose Session and Stateless for the Bean Type.
    7. Select database.BookDBEJBImpl for Enterprise Bean Class.
    8. In the Remote Interfaces box, select database.BookDBEJBHome for Remote Home Interface and database.BookDBEJB for Remote Interface.
    9. Enter BookDBEJB for Enterprise Bean Name.
    10. Click Next and then click Finish.
  9. Add a resource reference for the Cloudscape database to the BookDBEJB bean.
    1. Select the BookDBEJB enterprise bean.
    2. Select the Resource Refs tab.
    3. Click Add.
    4. Select javax.sql.DataSource from the Type column.
    5. Enter jdbc/BookDB in the Coded Name field.
  10. Save BookDBJAR.
    1. Select BookDBJAR.
    2. Select FileSave As.
    3. Navigate to the directory examples/build/web/ejb.
    4. Enter bookDB.jar in the File Name field.
    5. Click Save EJB JAR As.
  11. Add a reference to the enterprise bean BookDBEJB.
    1. Select Bookstore2WAR.
    2. Select the EJB Refs tab.
    3. Click Add.
    4. Enter ejb/BookDBEJB in the Coded Name column.
    5. Select Session in the Type column.
    6. Select Remote in the Interfaces column.
    7. Enter database.BookDBEJBHome in the Home Interface column.
    8. Enter database.BookDBEJB in the Local/Remote Interface column.
  12. Specify the JNDI Names.
    1. Select Bookstore2App.
    2. In the Application table, locate the EJB component and enter BookDBEJB in the JNDI Name column.
    3. In the References table, locate the EJB Ref and enter BookDBEJB in the JNDI Name column.
    4. In the References table, locate the Resource component and enter jdbc/Cloudscape in the JNDI Name column.
  13. Enter the context root.
    1. Select the Web Context tab.
    2. Enter bookstore2.
  14. Deploy the application.
    1. Select ToolsDeploy.
    2. Click Finish.
  15. Open the bookstore URL http://<host>:8000/bookstore2/enter.

No comments:

Post a Comment