Sample J2EE Applications


Chapter 3   SilverBooks

SilverStream provides the SilverBooks sample application to illustrate the Model-View-Controller design pattern. This page describes the SilverBooks application. It covers the following topics:

 
Top of page

What is SilverBooks?

SilverBooks is a Web-based e-commerce application that simulates an online bookstore. Users can interact with this application to purchase books from SilverBooks Corporation, a fictitious bookseller.

SilverBooks is implemented using J2EE standards and the Struts Framework architecture. Struts Framework is an open-source framework for Web applications that implements the Model-View-Controller (MVC) design paradigm. As an application that conforms to this paradigm, SilverBooks includes the following components:

Component

Description

Model

A set of form classes that hold the data that is updated by JavaServer Pages (JSPs).

View

JavaServer Pages (JSPs) that render the front-end interface. These JSPs insert dynamic content based on the interpretation of action tags at page request time.

Controller

An action servlet that dispatches requests to action classes to perform business logic, such as searching for a book or displaying book details when a user clicks a book title. The action servlet also provides the View component with the user interface to be displayed after the action is completed.

The major benefit of this approach is that it logically separates the key functions of the application--actions, data access, and presentation--to allow role-specific assignments, ease of maintenance, and efficient debugging.

 
Top of page

Technologies used in SilverBooks

SilverBooks incorporates J2EE standard technologies to implement the Struts Framework and Model-View-Controller (MVC) design paradigm. The following technologies are described:

    For more information about Struts Framework and the MVC design paradigm, see the Struts Framework Project Web site.

 
Top of section

JavaServer Pages

JavaServer Pages (JSPs) are used to implement the View component of the MVC design paradigm. SilverBooks uses JSPs to present both the static and the dynamic content of the bookstore application.

Static   The following JSPs display data that changes infrequently if at all in SilverBooks:

In fact, because the navigation bars never change, these two JSPs are reused throughout the application.

Dynamic   A more dynamic use of JSPs in SilverBooks is for presenting details about a book that has been selected from search results by the end user. When a user clicks a book title hyperlink, control passes to a JSP called bookdetail.jsp. This JSP in turn calls properties on a JavaBean called BookBean.java to determine how to present data about the book the user selected.

    For more information about JSPs, see the chapter on using JavaServer Pages in the Programmer's Guide of the server's Classic Development Help.

 
Top of section

Action servlets

The action servlet is the cornerstone of the Controller component of the MVC design paradigm. In SilverBooks, the action servlet performs three key functions:

  1. Receives requests from the client--in this case, a user interacting with the SilverBooks application in a Web browser

  2. Determines the action to perform

  3. Delegates to an appropriate View component the responsibility for producing the next part of the user interface

Two XML specifications are used to configure the action servlet. The specification in web.xml associates the URL pattern *.do with the action servlet. This association means that any URL request or post ending in .do will go to the action servlet for processing. The specification in action.xml maps each possible client request to an action class that performs the appropriate business logic or handles the possible exceptions.

For example, here is one of the URL mappings in action.xml:

  <action path="/bookDetail" actionClass="com.sssw.demo.silverbooks.action.DetailAction">
  <forward name="success" path="/bookdetail.jsp" />
  </action>

This mapping means that when the URL bookDetail.do is requested, the action servlet instantiates DetailAction class (or uses one that has already been instantiated) to display the detail data about a book selected by the user. The book identifier is passed as a parameter on the URL. This action class has access to a forward-mapping address called success which is mapped to the relative URL bookdetail.jsp.

The DetailAction class services the client request as follows:

  1. Gets a reference to the book in the EJB data source, based on the book identifier. Each book is represented by a book bean

  2. Retrieves the book data and places the book in the session

  3. If no exceptions occur, passes control to the JSP page bookdetail.jsp, which calls properties on the book bean to determine how to display the page

The shopping cart works in a similar way. When a user decides to add a book to the shopping cart, the URL cart.do is requested, causing the action servlet to pass control to the AddCartAction class along with two parameters on the URL: add as the action, and the book identifier.

 
Top of section

Form classes

Form classes represent the Model component of the MVC design paradigm. For example, the check-out and account detail pages use the form classes PaymentForm.java and AccountDetailForm.java to store data about customers' orders. When the data is updated, the application calls the validate() method on these form classes to ensure the accuracy of the information.

 
Top of section

Enterprise JavaBeans

Enterprise JavaBeans (EJBs) represent business logic and manage data access in the MVC paradigm. EJBs are referenced by action servlets and form classes.

In SilverBooks, EJBs perform two key functions:

The SilverStream server provides the connections to these databases.

    For more information about EJBs, see the chapter on using EJBs with SilverStream applications in the Programmer's Guide of the server's Classic Development Help.

 
Top of page

Internationalization

The Struts Framework provides a methodology for internationalizing prompts and messages in a Web application. SilverBooks implements this feature by using Java resource bundles that use name/value pairs to translate static text labels to the language selected by the user. Each language supported by SilverBooks has its own resource bundle.

To select a new language:

  1. Start the SilverBooks application by entering the following URL in your browser for Sybase Adaptive Server Anywhere databases:

    http://server/SilverBooks/app

    or for Informix Cloudscape databases, enter this URL:

    http://server/SilverBooksCS/app

  2. In the top navigation bar, click Select Language.

  3. Select the radio button associated with your language of choice and click Go.

    The page refreshes to display static text labels in the language you selected.

 
Top of page

Getting started

The SilverBooks sample application includes source code, ready-to-deploy archives and databases with the archives already installed. You'll find these files in the SilverStream\samples\SilverBooks directory. You'll notice that there is a set of files for the Informix Cloudscape database in the \Cloudscape subdirectory and a set of files for the Sybase Adaptive Server Anywhere database in the \SybaseASA subdirectory. There is also a directory containing the source files.

You can run either version of the application by adding the appropriate SilverBooks database to the SilverStream server.

About the other files provided   The Sybase ASA and Cloudscape subdirectories also include these files:

File or directory

Description

buildSilverBooksAll.bat

Builds (but does not deploy) all of the application components).

buildSilverBooksEar.bat

Archives all of the component EJB Jars and the War file and builds the EAR file.

deploySilverBooksEar.bat

Deploys the SilverBooks EAR file to the SilverStream server. You must restart the server after running this file.

silverbooks.ear

The SilverBooks application archive.

silverbooks.plan.xml

The XML deployment plan for the Silverbooks.ear

You can use run these files if you make changes to the application or want to run with debug on. For more information on using these files, see Modifying the SilverBooks databases

To run SilverBooks:

  1. After installing the SilverBooks database, open your browser.

  2. Enter this URL for the Sybase Adaptive Server database version:

    http://server/SilverBooks/app

    Use this URL for the Cloudscape database version:

    http:///server/SilverBooksCS/app

    The home page opens in your browser:

  3. Interact with the SilverBooks application by:

 
Top of page

Modifying the SilverBooks databases

In most cases, you will only need to run the application as described in Getting started. If you want to work with the database by either of the following:

you need to rebuild and redeploy the application.

To rebuild the application:

  1. Open a command prompt and change to the following directory for Cloudscape:

      %SilverStream_install_directory%\samples\SilverBooks\Cloudscape
    

    or this directory for Sybase Adaptive Server Anywhere:

      %SilverStream_install_directory%\samples\SilverBooks\SybaseASA
    
  2. Run the following batch file:

      buildSilverBooksAll
    

    The buildSilverBooksAll command should not generate any errors.

    Now you redeploy the application.

To redeploy the application:

  1. Open a command prompt and change to the following directory for Cloudscape:

      %SilverStream_install_directory%\samples\SilverBooks\Cloudscape
    

    or this directory for Sybase Adaptive Server Anywhere:

      %SilverStream_install_directory%\samples\SilverBooks\SybaseASA
    
  2. Run the following batch file:

      deploysilverbooksear %ServerName%:%Port%
    

Sample J2EE Applications

Copyright © 2001, SilverStream Software, Inc. All rights reserved.