Now that you have all of the interfaces and the deployment descriptor finished, it is time to
create a jar file containing all of the class files, and the ejb-jar.xml file in the META-INF
directory. After this jar file is complete, you must follow the deployment directions specific to
your application server. This topic is discussed in a later section, “Deploying Your EJB to Your
Client View of a Session Bean
We have talked quite a bit about what goes into the writing of an EJB and session beans. Now
we will finish the picture by discussing how a client accesses an EJB.
The first thing that any client must do is locate the home interface for the session bean it
requires. If you remember, the home interface is stored in a JNDI service, so we must use a
JNDI context to look it up. The code looks like this:
Context initialContext = getInitialContext();
CartHome cartHome =
Creating the InitialContext for the JNDI lookup can be specific to the application server you
are using. In general client code, it requires that a Context factory class be passed to it.
However, when another EJB is acting as the client, other bean home interfaces can be deployed
into the bean's environment.
Deploying a home interface into another bean's environment is similar to the variable you
deployed earlier. When this is done, the lookup becomes easier because the client already has
some knowledge of the home interface.
The method of casting the object returned from the JNDI lookup is unique. You will notice that
the static narrow() method of the PortableRemoteObject class is used prior to casting the
home interface with a simple casting operator. According to the Enterprise JavaBeans
Specification, this method of narrowing the remote object is required if your code is to be used
among different EJB-compliant containers. It also states that programs that use the casting
operator to narrow the remote and home interfaces are likely to fail if the current container is
using RMI/IIOP as the underlying communication. Because many of the most popular applica-
tion servers implement their EJB container using RMI/IIOP, it is strongly recommended that
you narrow the interfaces using this method.
After the client has obtained the home interface it requires, it must obtain the remote interface
before it can perform any business logic calls to the bean. Because you have developed a state-
less session bean, you really only have one choice, and that is the no-argument create()
method. The create() method will return the remote interface which gives you access to the
business logic of the session bean.