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); ...

Get Sams Teach Yourself J2EE™ in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.