The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Creating the Enterprise Bean

The source code for the enterprise bean is in the examples/src/ejb/converter directory. An enterprise bean is a server-side component that contains the business logic of an application. At run time, the application clients execute the business logic by invoking the enterprise bean's methods.

Coding the Enterprise Bean

The enterprise bean in this example requires the following code:

Coding the Remote Interface

A remote interface defines the business methods that a client may call. The business methods are implemented in the enterprise bean code. The source code for the Converter remote interface follows.

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Converter extends EJBObject {

public double dollarToYen(double dollars) throws
                                        RemoteException;
public double yenToEuro(double yen) throws RemoteException;
} 

Coding the Home Interface

A home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The ConverterHome interface contains a single create method, which returns an object of the remote interface type. Here is the source code for the ConverterHome interface:

import java.io.Serializable;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface ConverterHome extends EJBHome {

    Converter create() throws RemoteException, CreateException;
} 

Coding the Enterprise Bean Class

The enterprise bean in our example is a stateless session bean called ConverterEJB. This bean implements the two business methods, dollarToYen and yenToEuro, that the Converter remote interface defines. The source code for the ConverterEJB class follows.

import java.rmi.RemoteException; 
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class ConverterEJB implements SessionBean {

   public double dollarToYen(double dollars) {

      return dollars * 121.6000;
}

   public double yenToEuro(double yen) {

      return yen * 0.0077;
}

   public ConverterEJB() {}
   public void ejbCreate() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sc) {}
} 

Compiling the Source Files

Now you are ready to compile the remote interface (Converter.java), home interface (ConverterHome.java), and the enterprise bean class (ConverterEJB.java):

  1. Go to the examples/src directory.
  2. Open the file build.xml in a text editor. Edit the following line, setting the value of the j2ee-home property to the J2EE SDK installation directory:
    <property name="j2ee-home" value="C:\j2sdkee1.3" /> 
    
  3. In a terminal window type the following command:
    ant converter 
    

This command compiles the source files for the enterprise bean and the J2EE application client. It places the resulting class files in the examples/build/ejb/converter directory. For more information about ant, see How to Build and Run the Examples.


Note: When compiling the code, the preceding ant task includes the j2ee.jar file in the classpath. This file resides in the lib directory of your J2EE SDK installation. If you plan on using other tools to compile the source code for J2EE components, make sure that the classpath includes the j2ee.jar file.

Packaging the Enterprise Bean

In this section you will run the New Enterprise Bean Wizard of the deploytool to perform these tasks:

To start the New Enterprise Bean Wizard, select File->New Enterprise Bean. The wizard displays the following dialog boxes.

  1. Introduction Dialog Box
    1. Read this explanatory text for an overview of the wizard's features.
    2. Click Next.
  2. EJB JAR Dialog Box
    1. In the combo box labelled Enterprise Bean Will Go In, select ConverterApp.
    2. In the JAR Display Name field enter ConverterJAR.
    3. Click the Add button next to the Contents text area.
    4. In the tree under Available Files, locate the examples/build/ejb/converter directory. (Or, you may type the directory name in the top field.)
    5. Select the following classes from the text area and click Add: Converter.class, ConverterEJB.class, and ConverterHome.class. (Or, you may drag them to the bottom field.)
    6. Click OK.
    7. Click Next.
  3. General Dialog Box
    1. Under Bean Type, select the Session radio button.
    2. Select the Stateless radio button.
    3. In the Enterprise Bean Class combo box, select ConverterEJB.
    4. In the Home Interface combo box, select ConverterHome.
    5. In the Remote Interface combo box, select Converter.
    6. In the Enterprise Bean Name field, enter ConverterBean.
    7. Click Next.
  4. Environment Entries Dialog Box

Because you may skip the remaining dialog boxes, click Finish.

Home
TOC
PREV TOC NEXT Search
Feedback