Prev | Next

TOC | Index

J2EETM Developer's Guide
Getting Started


Coding the Enterprise Bean

Every enterprise bean 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 Enterprise Bean's Source Code

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

UNIX:

1. In the following script, compileEJB.sh, change <installation-location> to the directory in which you installed the J2EE SDK.

#!/bin/sh

J2EE_HOME=<installation-location>
CPATH=.:$J2EE_HOME/lib/j2ee.jar

javac -classpath "$CPATH" ConverterEJB.java ConverterHome.java Converter.java
2. Run the compileEJB.sh script.

Windows:

1. In the following script, compileEJB.bat, change <installation-location> to the directory in which you installed the J2EE SDK.

set J2EE_HOME=<installation-location>
set CPATH=.;%J2EE_HOME%\lib\j2ee.jar

javac -classpath %CPATH% ConverterEJB.java ConverterHome.java Converter.java
2. Run the compileEJB.bat script.



Prev | Next

TOC | Index


Copyright © 2000 Sun Microsystems, Inc. All rights reserved.