The Java Naming and Directory Interface – (JNDI) is a Java Application Program Interface for accessing (look up), adding, modifying and deleting the objects in different naming and directory services. In this article we would give an introduction to; JNDI architecture, different object storing techniques, the roles of JNDI in various java technology, sample codes etc.
As mentioned in the definition, JNDI can access different naming and directory services like NIS, LDAP, Novell NetWare, CORBA,RMI etc. Name services and directory services are used by the clients to access objects using JNDI names. Name service contains the bindings which relate a name to an object where as Directory services can also provide attributes to the objects. So in directory service user can access object not only by using name but also would allow the user to search and filter the data. This means that the user can specify some properties and filter the objects just like the ‘where’ condition in the SQL ‘select’ statement.
Architecture
In the figure below, it is clear that a java application can access the various name and directory services through JNDI API. Actually it’s Service Provider Interface (SPI) which provides these services and the java applications can access it through JNDI API.

Different Object Storing Techniques
We can store the objects in JNDI services in any of the given three formats, they are serialized format, reference addresses and as attributes on a context. Storing serialized format is the simplest technique. In case of storing reference address, if we had modified the object after binding and then look up the data using JNDI name, we will get the modified data. In certain situation the object need to be accessed by some non java applications; storing attribute technique will be helpful here.
The roles of JNDI in Various Java Technology
We use the JNDI in various java technologies like; The Java Database Connectivity package-(JDBC), The Java Messaging Service-(JMS), and Enterprise JavaBeans-(EJB). As per JDBC specification JNDI is used to store the ‘DataSource’. We can look up the ‘DataSource’ using the JNDI name and using this ‘ DataSource’ we can make a connection. In JMS, configuration information is stored in administrated objects and the JNDI technique is used by the clients to access these objects. In Enterprise JavaBeans the client makes use of JNDI to access a particular enterprise bean.
Sample codes
A ‘context’ is a set of bindings of name to an object. The starting operation of a name or directory service is the ‘Initial context’.
An example to call the ‘initial context’ constructor is given below:-
Context initial = new InitialContext();
For looking up an object we can use the following codes:-
Object ref = initial.lookup(jndiName);
To listing a context:-
NamingEnumeration list1 = initial.list(contextName);
To add,replace and remove a bind:-
Book book = new Book(“Java”);

initial.bind(“favorite”, book);//To add

Book book = new Book(“C”);

initial.rebind(“favorite”, book);//To replace

initial.unbind(“favorite”);//To remove

To Rename an object:-
initial.rename(“favorite”, “old-favorite”);
To create and destroy a context:-
Book book = new Book(“Java”);

Context result = initial.createSubcontext(“new”);//To create

initial.destroySubcontext(“new”);//To destroy

Conclusion
There are many possibilities that could be achieved using JNDI. JNDI can be integrated into many java technologies that we normally use nowadays as mentioned above. Mostly the programmers are not fully aware of the JNDI techniques. There are lots to learn of JNDI. This small article is just a window to all the endless milestones that we could achieve through JNDI.