March 8, 2021
Hot Topics:

Introduction to EJBs: Part 4

  • By Sams Publishing
  • Send Email »
  • More Articles »

This is the fourth of four installments that make up Chapter 4: Introduction to EJBs from the book Sams Teach Yourself J2EE in 21 Days (ISBN:0-672-32384-2) written by Martin Bond, Dan Haywood, Debbie Law, Andy Longshaw, and Peter Roxburgh, published by Sams Publishing.

Deploying and Using an EJB in the J2EE Reference Implementation

You should now be in a position to write and test an EJB client. However, before you can do that, you must deploy an EJB that it can use. In this section, you will look at how to deploy an EJB in the J2EE Reference Implementation (RI) and how to then use it from a simple client.

The J2EE on which your EJB is deployed will provide a complete server-side environment. It houses any EJBs, runs a Web Server for JSP/servlets, runs a naming server for storing component location information, and provides database access. All J2EE- compliant application servers will do this—even a non-commercial version, such as the J2EE RI. The RI also provides you with a ready-to-use database so you do not have to concern yourself with hooking up to an existing database or installing a separate one.

To deploy and test your EJBs (and servlets/JSPs later), you only need a single machine. Both the J2EE client and the J2EE server (and its EJBs, servlets and JSPs) can run on the same machine. No connection to the Internet is required. The J2EE RI is available on multiple platforms (Win32, Solaris, and Linux) and should be consistent across these platforms, so that J2EE applications created on one platform can be deployed on another.

If you encounter problems at any stage, try referring to the troubleshooting section just before today's Summary.


Before running any of the tools described in this section, you will need to set the J2EE_HOME environment variable to the location on your hard drive where you deployed the J2EE reference implementation. You should also add the bin directory below J2EE_HOME to your executable search path (%PATH% under Windows or $path under Unix/Linux) so that you can run J2EE tools and batch files from the command line.

Opening the Case Study EAR File

To deploy and manipulate EJBs under the RI, you will use a graphic tool called deploytool. Before you start using this, you will need to do two things:

  1. Ensure that you have created and configured your database environment as described on Day 2.

  2. Start the J2EE RI runtime environment and the associated Cloudscape database. To do this, run the cloudscape and j2ee scripts/batch files found in the bin directory under J2EE_HOME as follows:

  3. cloudscape -startj2ee -verbose

    The use of the -verbose flag for J2EE is not strictly necessary, but you may find it useful to help you understand what the J2EE server does when it starts up.

Now you are ready to run the deploytool. Again, this is a script/batch file found in the bin directory under J2EE_HOME. When you run it, the GUI screen will appear as shown in Figure 4.3.

You should now be able to open the initial agency enterprise archive provided in the JAR subdirectory of the Day 4 exercise code on the CD-ROM (agency.ear). Do this through the menus by selecting File, Open and then browsing for the file in the subsequent Open Object dialog box. Select the EAR file and click the Open Object button. The agency application will now be displayed in the list of applications, as shown in Figure 4.4.

All of the code for the Agency EJB that is contained in the Agency application can be found in the agency subdirectory of the src directory under the Day 4 Exercise part of the CD-ROM.

Now that the enterprise application is loaded in deploytool, you can examine its settings.

Figure 4.3
The initial screen shown by deploytool.

Figure 4.4
The Agency application has now been loaded by deploytool.

Examining the Case Study Application

You can use deploytool to examine and alter deployment descriptor information for the application and, if necessary, for individual components, such as EJBs.

If you select the JNDI Names tab for the agency application, you will see information about the resources that the application exports and consumes. This is largely based on information defined in the application deployment descriptor and the container-specific deployment descriptor described earlier in the Enterprise Applications section.

In Figure 4.5, you can see in the Application box that there is a single EJB in this initial form of the application. That EJB can be referenced through JNDI using the name ejb/Agency.

Figure 4.5
deploytool displays the JNDI information from the Agency application deployment descriptor.

In the References box, you can see that two of the components in the application use external resources. First, you can see that the component named Agency (the EJB) uses a resource called jdbc/Agency that is registered under JNDI as jdbc/Agency.

The References box also indicates that the application client, SimpleClient, references the Agency EJB by using the name ejb/Agency that appears under JNDI as ejb/Agency.

You can also examine the settings of the EJB through deploytool. Click the icon next to the Simple JAR file symbol to show the EJBs contained in the Simple EJB-JAR file. There is a single EJB in the JAR file called Agency. If you select the Agency EJB, you will see the properties defined in the deployment descriptor for that EJB. Select the Resource Refs tab to see what external resources this EJB uses, as shown in Figure 4.6.

Figure 4.6
You can examine the deployment descriptor information for a single EJB, such as the external resources it expects.

Figure 4.6 shows that the Agency EJB expects one resource called jdbc/Agency that is of type javax.sql.Datasource. This is the EJB deployment descriptor information you saw in Listing 4.5.

Figure 4.7 shows the environment entries for the Agency EJB. If you want to alter the AgencyName defined there, you can just double-click the Value field and type in an alternative name. If you make any changes to the configuration of the application or any of its components, the suffix (changed) will be added to the application name in the title bar.

Figure 4.7
Environment entries can be viewed or edited through deploytool.

Page 1 of 2

This article was originally published on August 1, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date