blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

LDAP Operation: Read selected attributes

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

try {
 //In the main() method of the program, create an initial directory context. 
 //This is similar to creating an initial context in the previous naming example, 
 //except that you use the constructor for InitialDirContext
 Hashtable env = new Hashtable();
 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");   env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=csRepository");
 
 DirContext ctx = new InitialDirContext(env);
   
 // Ask for all attributes of the object 
    Attributes attrs = ctx.getAttributes("cn=John Eipe, ou=employees");
     
    // Find the surname attribute ("sn") and print it
    System.out.println("sn: " + attrs.get("sn").get());
    System.out.println("mail: " + attrs.get("mail").get());
 }catch(NamingException e){
  e.printStackTrace();
 }

Output:
sn: Eipe
mail: john77eipe@gmail.com

Continue reading: LDAP Operations

No comments:

Post a Comment