JNDI Example-Accessing LDAP in Java

We have discussed the fundamentals of JNDI and a  naming example in the previous sections.In this chapter we are looking into how the accessing LDAP in Java.JNDI plays the key role in looking up the directory objects in the LDAP server.Let us start with a quick overview on LDAP. The example shows how JNDI is using to access directory objects.

Overview to LDAP

The name LDAP stands for Light weight Directory Access protocol . LDAP is a protocol for maintaining distributed directory information services over internet.Directory services provides organized set of records in a hierarchical order(Example : Telephone directory) . In this chapter we are discussing how to access the directory attributes from an LDAP server using a Java program which uses JNDI.

Accessing LDAP using JNDI

Before proceeding to the example we should have an accessible LDAP server running . We are running an LDAP server in our local machine.When the server is ready we need to create a directory object and we need to add few attributes to the object . After those things completed , we can write the Java code to access the attribute values of directory object we created  .We are using Apache Directory Studio for creating and managing LDAP server. (Alternately we can use eclipse for managing LDAP. For that we need to update eclipse with the URL :http://directory.apache.org/studio/update/2.x  ). Here we are using Apache Directory Studio for managing LDAP.

Let us start our example from scratch.We need to setup the LDAP server  and directory objects using Apache Directory Studio first. Then we need to write the Java code for accessing the LDAP. LDAP support is  existing with JDK itself.So ,no third party API is  needed.We are discussing all the procedures to access LDAP from Java in steps.

Step 1)If JDK is not installed ,please download and install.

Step2)If eclipse indigo (or any other eclipse distribution) is not installed , please download and extract to any folder and open it by clicking on the icon.

Step3)Download and install Apache Directory Studio.

Step4)Open Apache Directory Studio.

Step5)Right click on the server console of directory studio.Select  New–>New server

Step6)Create a new server with suitable name(In our example it is LocalLDAPServer)

Step7)Run the server by right clicking on the server instance.

Step 8)When the server is started , right click on the server instance and select create connection option.