Gopalan Suresh Raj's Web Cornucopia
An Oasis for the parched Enterprise Services Engineer/Developer

    Container Managed Models-Enterprise JavaBeans - Part 4


    Developing an N-tier EJB Application

    How can you provide an EJB-based business solution by integrating session and entity beans? Assume you are a consulting firm that was just asked to develop an online store for a fictitious bookstore chain called Horses and Stable, Inc. This bookstore chain now wants to expand its business to the Web. Now suppose Horses and Stable Online carries about 5 million books and sells tens of thousands of copies of books every day.

    Horses and Stable Online offers extensive book catalogs, as well as a variety of other resources including customer reviews, personal recommendations, and gift suggestions. When customers visit Horses and Stable Online, they should be able to:

    • Browse for books in 28 subject areas and browse for music within hundreds of genres in 14 different browse rooms.
    • Maintain a dynamic shopping cart by adding books and CDs, which they may opt to check out later.

    While the previous scenario is hypothetical, you are likely to find similar requirements that need similar solutions in the real world. Throughout much of this chapter, we will gradually develop the functionality for this online bookstore, while explaining the concepts behind building and deploying powerful EJB applications

    To develop the example, you must have the appropriate deployment tools and an EJB server. The deployment tools are used to generate enterprise bean containers, which are the classes that provide an interface to the low-level implementations in a given EJB server.

    All the EJB source code follows certain naming conventions to help the EJB server find the application classes, containers, and database tables. For example, the source code that constitutes enterprise beans—the enterprise bean class, home interface, and primary key class names—use the remote interface name as a prefix. These naming conventions are discussed as we go along. While source file-naming conventions are standard across EJB server implementations, naming conventions used within the implementation sources are specific to the EJB server or other deployment tools.

    All these examples make use of the EJB server “HomeBase” v0.5.1 (formerly called “EJBHome”—not to be confused with the javax.ejb.ejbhome interface) from Iona, the makers of the popular CORBA ORB Orbix. You can download a free implementation of HomeBase, as well as deployment tools for generating containers from You can also, of course, opt to use any other EJB Server and deployment tools as long as you know how to modify your deployment descriptors to match the particular EJB server with which you are working. Similarly, you need a database and you should know how to set up the data source for your platform.

    Assuming you follow through, by the end of this article, you will have built an online store from which books and music CDs can be purchased over the Web.

    The code used for developing our online store consists of:

    1. A couple of entity beans (Books and Music entity beans).
    2. A session bean (Cart session bean) that creates and manages the entities.
    3. A servlet client (Online servlet) that creates a Cart and mimics an online store.
    4. An entity bean client to test out our entities.
    5. A session bean client to test out our session bean.
    6. A Microsoft Access database (Shop.mdb) containing the store inventory.

    The following Figure 6, shows the architecture for our online bookstore. The Web browser client starts the whole process by initiating a session and sending a GET message to the Online servlet, which resides on a Web server. The init() method of the Online servlet class looks up the Cart EJB from the EJB server and creates the Cart EJB. The Cart EJB, in turn, creates a Books Entity bean and a Music Entity bean that talk to the database to get the work done.

    Fig 6: The four-tier architecture for our online bookstore

    Fig 6: The four-tier architecture for our online bookstore

    Author Bibliography

    Gopalan Suresh Raj is a Senior Analyst, Software Architect, and Developer with expertise in multi-tiered systems development, enterprise service architectures, and distributed computing. He is also an active author, including contributions to Professional JMS Programming, Wrox Press, 2001, Enterprise Java Computing-Applications and Architecture, Cambridge University Press, 1999, and The Awesome Power of JavaBeans, Manning Publications Co., 1998. He has submitted papers at international fora, and his work has been published in numerous technical journals. Visit him at his Web Cornucopia© site ( or mail him at