Home > how do I?, Java, JNDI, LDAP > Use JNDI to access an LDAP (part 2).

Use JNDI to access an LDAP (part 2).

In the previous part, we began working with the code to create a connection to an LDAP. The code is listed completely in parts with the core of it below.  See the box.net widget in the blog here to obtain the LDAPCtx.java code.

  1. The DirectoryContext is going to be defined from the env variable, which is the Hashtable containing our environment variables.

    DirContext ctx = new InitialDirContext(env);

  2. Next we define the Attributes we are going to be looking for. Attributes is an interface which represents a collection of values (attributes) associated with an object.

    Attributes matchAttrs = new BasicAttributes(true);

  3. The next line includes BasicAttribute which implements the Attributes interface with an unordered attribute and its value. In this instance, the attribute is “uid” and its value is”Vice.Pres.”

    matchAttrs.put(new BasicAttribute("uid","Vice.Pres"));

  4. Now we implement the interface NamingEnumeration to contain the results of searching our connection for our given attributes value pair.

    NamingEnumeration answer = ctx.search("ou=People", matchAttrs);

  5. Finally, while there are results in our enumeration, we print them out to the console.

    while(answer.hasMore())
    {
    SearchResult sr = (SearchResult)answer.next();
    log.debug(">>>" + sr.getName());
    }

Advertisements
Categories: how do I?, Java, JNDI, LDAP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: