Lightweight Directory Access Protocol (LDAP) is a standard protocol that can be used to
access a directory structure. Currently, it is the most popular protocol available. For this reason,
many directory vendors provide access to their server through LDAP.
Any of the LDAP-compliant servers can be accessed using LDAP, and to the client can seem to
be working the same way. This does not mean that they store and handle the information the
same way internally, only that they will return it to the LDAP client in a specific manner.
The Java Naming and Directory Interface (JNDI) provides a standard API for interacting with
naming and directory services. JNDI allows a Java client to interact with several different
directory protocols using this standard API.
JNDI places another layer of abstraction on top of the directory protocol. This is accomplished
by using the JNDI API with the appropriate service provider plugged in to talk to your direc-
tory API. By doing this, the client Java code only needs to be aware of the JNDI API. By plug-
ging in different service providers, your client can switch from LDAP to Novell's NDS, for
example, without changing the client code.
This is a very powerful design pattern that is widely used throughout Java. Another example is
Java Database Connectivity (JDBC), which provides a standard API to communicate with
It is because of this flexibility, and the fact that JNDI is a standard API, that JNDI is the proper
choice for communicating with LDAP servers from within Java code.
Using JNDI to Access LDAP
With a basic understanding of LDAP and JNDI, we can take a look at how to use the two
together to access information stored in a directory. In this section we will take a look at
searching, adding, modifying, and deleting objects stored in an LDAP server.
You will develop two utility classes that can be used with any LDAP server. The first class will
contain all of the code to perform the LDAP operations. This class will be named
LDAPManager . Sections of this class will be revealed in pieces and explained; the full listing for
LDAPManager.java is found later in the chapter in Listing 11.2.
The examples in this chapter were developed using Netscape Directory Server (NDS). To work
through the examples, you'll need to download, install, and configure NDS as described in the