The J2EETM Tutorial
Home
TOC
Index
PREV TOP NEXT 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.

Table 11-1 Duke's Bookstore Example JSP Pages 
Function
JSP Pages
Enter the bookstore
bookstore.jsp
Create the bookstore banner
banner.jsp
Browse the books offered for sale
catalog.jsp
Put a book in a shopping cart
catalog.jsp and bookdetails.jsp
Get detailed information on a specific book
bookdetails.jsp
Display the shopping cart
showcart.jsp
Remove one or more books from the shopping cart
showcart.jsp
Buy the books in the shopping cart
cashier.jsp
Receive an acknowledgement for the purchase
receipt.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 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/bookstore2 directory created when you unzip the tutorial bundle (see Downloading the Examples). 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. Chose 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.

See Troubleshooting for help with diagnosing common problems.

Home
TOC
Index
PREV TOP NEXT Search
Feedback