class GetAttrib {
public static void main(String[] argv) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
// specify where the ldap server is running
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=javacourses.com,c=Canada");
// use simple authenticate to authenticate the user
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "password");
try {
// Create the initial directory context
DirContext ctx = new InitialDirContext(env);
// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("cn=Qusay Mahmoud");
// Find the surname ("sn") attribute of this object and print it
System.out.println("Last Name: " + attrs.get("sn").get());
// Close the context
ctx.close();
} catch (NamingException e) {
System.err.println("Problem getting attribute: " + e);
}
}
}