Accessing the session bean using JNDI

JNDI can also be used to access an EJB but using this technique is not as easy as DI. The Salutation EJB from the first recipe and the servlet from the second recipe are used to illustrate this technique.

Getting ready

To use JNDI in a client:

  1. Obtain an InitialContext object
  2. Use the context to look up the EJB

    Familiarize yourself with the Salutation session bean and its two methods as developed in the Creating a Simple Session EJB recipe. We will also modify the SalutationServlet from the second recipe to use JNDI instead of DI.

How to do it...

A portable JNDI name syntax has been added to EJB 3.1. We will use this syntax as it makes the use of JNDI less dependent on the deployment server.

The modification of the ...

Get EJB 3.1 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.