O'Reilly logo

Sams Teach Yourself J2EE™ in 21 Days by Peter Roxburgh, Andy Longshaw, Debbie Law, Dan Haywood, Martin Bond

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

JNDI Name Lookup

The most common use of JNDI is to look up objects that have been bound to a name. To do this, you require two items of information:

  • The JNDI name

  • The class of the bound object

With this information in hand, object lookup is the simple matter of using the Context.lookup() method to find the object and then to cast that object to the required class.

Listing 3.2 shows a simple program to look up the name sams/book that was bound by the program in Listing 3.1.

Listing 3.2. Full Text of JNDILookup.java
 1: import javax.naming.*; 2: public class JNDILookup 3: { 4: private final static String JNDI = "sams/book"; 5: public static void main(String[] args) { 6: try { 7: Context ic = new InitialContext(); 8: String name = (String)ic.lookup(JNDI); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required