Chapter 11. EJB Client

Table of Contents

11.1. Overview
11.2. Programming a Client to Access EJB
11.3. Configuring InitialContext
11.3.1. Configuring Naming Attributes Using JVM Properties
11.3.2. Configuring Naming Attributes Using a Hashtable

This chapter describes an example of EJB client programming and how to configure the InitialContext object.

An EJB client is any kind of application that accesses and invokes business logic of EJB components deployed inside the EJB engine. EJB clients thus include Servlets, applets, other EJBs, standalone Java programs, etc.

This chapter covers the basic information needed to implement and execute an EJB client through a standalone Java program.

The following example illustrates the implementation of a stand-alone Java application client that looks up an EJB with the export name HelloApp and invokes a business method, sayHello(), on it.


InitialContext is an important component required for using the EJBs bound to the JNDI namespace.

It is used to look up all objects specified in the JNDI namespace. EJB uses it to search for actual bean references of the EJB engine. In order to obtain a JEUS InitialContext instance, you need to specify at least one of the five naming context attributes before attempting to acquire an InitialContext, so that your client application will be able to communicate with the JEUS Naming Server (JNSServer).

The following describes how to set up the JNDI InitialContext object either through the use of JVM "–D" properties (recommended approach) or through a hashtable in the EJB client code. It will also show how to actually invoke the EJB client.

The following are five supported naming context environment attributes that must be configured prior to acquiring a JEUS InitialContext object.

The attributes in the previous sub-section may be configured by using the "-D" switch of the JVM interpreter. The switch should be appended to the "java" command that is used to start the EJB client application.

The following shows how to configure each naming attribute using the "-D" switch.

The following example shows how to use the "-D" option to configure the naming context of an EJB client during its invocation. The example shows how a user named "user1" with the password "password1" is used for an EJB client that looks up EJB stubs through a JEUS naming server with the IP address, 192.168.10.10:

$ java -classpath
      %JEUS_HOME%/lib/client/jclient.jar
      -Djava.naming.factory.initial=jeus.jndi.JNSContextFactory
      -Djava.naming.factory.url.pkgs=jeus.jndi.jns.url
      -Djava.naming.provider.url=192.168.10.10
      -Djava.naming.security.principal=user1
      -Djava.naming.security.credentials=password1
      HelloClient

Note

A client library is needed, if the version of an EJB deployed on JEUS is 2.x. This library is usually generated when compiling EJB using an appcompiler.

If a class FTP server is used, clients may automatically download and use the necessary classes. None is required when using EJB version 3.0 or later.