import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;
public class AttributeExample {
public static void main(String[] argc) throws Exception {
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
DirContext dctx = new InitialDirContext(env);
Attributes attrs = new BasicAttributes(true);
attrs.put(new BasicAttribute("email"));
attrs.put(new BasicAttribute("website", "www.pri.com"));
NamingEnumeration result = dctx.search("ou=People", attrs);
while (result.hasMore()) {
SearchResult sr = (SearchResult) result.next();
System.out.println("Result = " + sr.getName());
Attributes srchAttrs = sr.getAttributes();
NamingEnumeration attributes = srchAttrs.getAll();
while (attributes.hasMore()) {
Attribute attr = (Attribute) attributes.next();
System.out.println("Attribute: " + attr.getID());
NamingEnumeration values = attr.getAll();
while (values.hasMore()) {
Object value = values.next();
System.out.println("Value = " + value);
}
}
}
}
}