Prev | Next

TOC | Index

J2EETM Developer's Guide
Getting Started

Building the Client

The ConverterClient program is a stand-alone Java application. To create ConverterClient you perform these steps:

1. Coding the Client

2. Compiling the Client's Code

Coding the Client

The source code illustrates the basic tasks performed by the client of an enterprise bean:

Locate the Home Interface

The ConverterHome interface defines life cycle methods such as create. Before the ConverterClient can invoke the create method, it must instantiate an object whose type is ConverterHome. This is a three-step process:

1. Create a JNDI naming context.

Context initial = new InitialContext();
2. Retrieve the object bound to the name MyConverter. (This is the JNDI name you specified in the section, "Deploying the J2EE Application".)

java.lang.Object objref = initial.lookup("MyConverter");
3. Narrow the reference to a ConverterHome object.

ConverterHome home = 
           (ConverterHome) PortableRemoteObject.narrow(objref,

Create an Enterprise Bean Instance

To create the ConverterEJB class, the client invokes the create method on the ConverterHome object. The create method returns an object whose type is Converter. The remote Converter interface defines the business methods in ConverterEJB that the client may call. When the client invokes the create method, the EJB container instantiates ConverterEJB, and then invokes the ConverterEJB.ejbCreate method.

Converter currencyConverter = home.create();

Invoke a Business Method

Calling a business method is easy. You simply invoke the method on the Converter object. The EJB container will invoke the corresponding method on the ConverterEJB instance that is running on the server. The client invokes the dollarToYen business method in the following line of code.

double amount = currencyConverter.dollarToYen(100.00);

ConverterClient Source Code

The full source code for the ConverterClient program follows.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import Converter;
import ConverterHome;

public class ConverterClient {

   public static void main(String[] args) {
       try {
           Context initial = new InitialContext();
           Object objref = initial.lookup("MyConverter");

           ConverterHome home = 

           Converter currencyConverter = home.create();

           double amount = currencyConverter.dollarToYen(100.00);
           amount = currencyConverter.yenToEuro(100.00);


       } catch (Exception ex) {
           System.err.println("Caught an unexpected exception!");

Compiling the Client's Code


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



javac  -classpath "$CPATH"
2. Run the script.


1. In the following script, compileClient.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%
2. Run the compileClient.bat script.

Prev | Next

TOC | Index

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