// Initial context implementation
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://localhost:389";
public static String MY_SEARCHBASE = "o=Airius.com";
public static String MY_FILTER = "(sn=Carter)";
public static void main(String args[]) {
try {
// Hashtable for environmental information
Hashtable env = new Hashtable();
// Specify which class to use for our JNDI provider
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
// Specify host and port to use for directory service
env.put(Context.PROVIDER_URL, MY_HOST);
// Get a reference to a directory context
DirContext ctx = new InitialDirContext(env);
// Specify the scope of the search
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Perform the actual search
// We give it a searchbase, a filter and a the constraints
// containing the scope of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,
MY_FILTER, constraints);
// Now step through the search results
while (results != null && results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
String dn = sr.getName();
System.out.println("Distinguished Name is " + dn);
Attributes attrs = sr.getAttributes();
for (NamingEnumeration ne = attrs.getAll();
ne.hasMoreElements(); ) {
Attribute attr = (Attribute) ne.next();
String attrID = attr.getID();