June 18, 2018
Hot Topics:

Locating Resources Using JNDI (Java Naming and Directory Interface)

  • June 2, 2003
  • By Sams Publishing
  • Send Email »
  • More Articles »


Contexts provide a hierarchical structure to JNDI names, and composite names group together related items. You have no doubt seen this same idea when using a file system where you might have a /usr directory for all the home directories of the users of a machine, such as /usr/mark and /usr/ruth.

The name used in Listings 14.1 and 14.2 (sams/book) is an example of a composite name. If you are looking up just one or two objects, you might want to use the full name each time. However, if you are looking up objects that are in the same context of a composite name, it is simpler to change to a subcontext and look up a simple name within that context. For example, if you wanted to look up two objects, sams/book and sams/book_cd, you can change to the sams context and perform two lookups, one for book and one for book_cd.

The subcontext is a name entry in the same way as any other name, and you look it up in exactly the same way as before. The return type of the object passed back from the lookup() method is javax.naming.Context. The code in Listing 13.3 is similar to Listing 13.2, and retrieves a name from a subcontext. Note that this example assumes that you have already run the code in Listing 13.1.

Listing 13.3 Looking Up Objects from a Subcontext (LookupObjectSubContext.java)

package com.conygre.jspdevhandbook.chapter13;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class LookupObjectSubContext
 public static void main (String[] args) 
    Context initialContext = new InitialContext();
    Context samsContext  = (Context) initialContext.lookup("sams");
    String stringObject  = (String) samsContext.lookup("book");
    System.out.println("Looked up: " + stringObject);
   catch (NamingException e) 
    System.err.println("Couldn't find an object bound to the name "
             + BindObject.TITLE);

For a given context you can also programmatically:

  • List its subcontexts by invoking its listBindings() method.

  • Create new subcontexts through its createSubcontext() method.

  • Destroy subcontexts with its destroySubcontext() method.


To give you a taste of things to come in later chapters, here is a simple JSP that obtains a reference to an EJB through JNDI. This example does not run through how to deploy the example because this is detailed in Chapter 17. However, if you are familiar with deploying an Enterprise Archive (an .ear file) in an application server, or if you have read Chapter 17 already, to run the example you will need the chapter13.ear file from the book Web site.

What you will see here are screenshots of the example running in the WebLogic application server from BEA, but of course you can run it equally well under Sun's free RI server if you do not have an application server available.

The code in Listing 13.4 is for the EJB in this example. Don't worry too much about most of the code for now. For the purposes of this chapter, the important thing to realize is that the EJB implements a getHello() method that returns the nauseatingly familiar string Hello World!.

Listing 13.4 The EJB That Provides a getHello() Method (HelloWorldBean.java)

package com.conygre.jspdevhandbook.chapter13;

import java.rmi.RemoteException; 
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HelloWorldBean implements SessionBean
 public String getHello()
   return "Hello World!";

 public void ejbCreate() {}
 public void ejbRemove() {}
 public void ejbActivate() {}
 public void ejbPassivate() {}
 public void setSessionContext(SessionContext sc) {}

The JSP in Listing 13.5 uses a simple scriptlet to lookup a reference to the EJB, which in this example is bound to the name MyHelloWorld.

Note - The use of scriptlets in JSPs is now discouraged. However, this example uses one to keep things simple. In practice, you would use a JavaBean rather than a scriptlet.

Listing 13.5 The JSP That Looks Up the MyHelloWorld EJB (HelloWorld.jsp)

<%-- The EJB's remote interface is "HelloWorld"; the JNDI name is "MyHelloWorld"

   Access this page through http://localhost:8000/HelloJNDI/Hello
           or   http://localhost:8000/HelloJNDI/HelloWorld.jsp

<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.rmi.PortableRemoteObject"%>
<%@ page import="com.conygre.jspdevhandbook.chapter13.HelloWorld" %>
<%@ page import="com.conygre.jspdevhandbook.chapter13.HelloWorldHome" %>

 <head><title>HelloWorld, JNDI</title></head>
  The EJB says,
   Context initCtx = new InitialContext();
   Object o = initCtx.lookup("MyHelloWorld");

   HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow(o,
   HelloWorld   hello    = home.create();
   String     theGreeting = hello.getHello();

You can see from Listing 13.5 that the code to look up the EJB is practically the same as the examples you saw in Listing 13.2 and Listing 13.3:

Context initialContext = new InitialContext();
Object o = initialContext.lookup("MyHelloWorld");

The only difference is that when you look up an EJB, you must first look up something called the home interface, and then use that to create an instance of the EJB. When you get hold of the home interface, you must use the PortableRemoteObject.narrow() method rather than a simple programmatic cast. The reasons behind this are explained in Chapter 17.

After you have the EJB, invoke its methods as you would for any other Java object:

String theGreeting = hello.getHello();

When executed using the URL http://localhost:7001/HelloJNDI/HelloWorld.jsp, the output should be along the lines of what is shown in Figure 13.5.

Figure 13.5
The output from the HelloWorld JSP (HelloWorld.jsp).

You might be wondering how the mapping between the EJB and its name of MyHelloWorld is set up. Within the chapter13.ear file there is a JAR file called ejb-jar-ic.jar that contains an XML document called weblogic-ejb-jar.xml. This latest XML file is specific to the WebLogic application server and maps the EJB (HelloWorldBean) to its JNDI name (MyHelloWorld):

<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN" 

When the application is deployed in WebLogic, you can use the WebLogic Server Console to view this information as in Figure 13.6.

Figure 13.6
The WebLogic server console.

Page 6 of 7

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date