title: Retrieve data from active directory
body: <p>I am developing a project in java and JSP that needs to access an active directory for login and retrieving some values. I have done the login part(authentication) but i am not able to get help over the retrieval part.I am completely new to active directory concept, so please aid me accordingly.
  I am giving the code of the java class that i have used for user authentication:</p>
  
  <pre><code> package classes;
  
      import java.util.Hashtable;
      import javax.naming.Context;
      import javax.naming.NamingException;
      import javax.naming.directory.DirContext;
      import javax.naming.directory.InitialDirContext;
  
      public class AD_Auth {
  
      private static final String CONTEXT_FACTORY_CLASS ="com.sun.jndi.ldap.LdapCtxFactory";
      private static final String domainName = "@xyz.IN";
      private static final String providerUrl = "ldap://srdeladc02.xyz.in:123";
  
      public static boolean authenticate(String username, String password) {
  
          try {
              Hashtable&lt;Object, Object&gt; env = new Hashtable&lt;Object, Object&gt;();
              env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY_CLASS);
              env.put(Context.PROVIDER_URL, providerUrl);
              env.put(Context.SECURITY_PRINCIPAL, username + domainName);
              env.put(Context.SECURITY_CREDENTIALS, password);
                          env.put(Context.SECURITY_AUTHENTICATION, "strong");
              DirContext ctx = new InitialDirContext(env);
              ctx.close();
  
              return true;
  
          } catch (NamingException e) {
              System.out.println("Dear User, The username or password you have entered is in correct, please try again.");
          }
  
          return false;
  
      }
  
      public static void main(String[] args) {
          // if someone emailid is myemail#icar.gov.in then username will be myemail
          //System.out.println(AD_Auth.authenticate("username", "password"));
  
      }
  
  }
  </code></pre>