title: Retrieve all users and their roles from LDAP using Java
body: <p>I have a Web application. For LDAP I am using <strong>Apache Directive Studio.</strong>
  I want to get all the users and their roles in my application.</p>
  
  <p>I am able to get particular information by using the following code.</p>
  
  <pre><code>    import java.util.Properties;
      import javax.naming.Context;
      import javax.naming.NamingException;
      import javax.naming.directory.Attributes;
      import javax.naming.directory.DirContext;
      import javax.naming.directory.InitialDirContext;
  
      public class DirectorySample {
          public DirectorySample() {
  
          }
  
          public void doLookup() {
              Properties properties = new Properties();
              properties.put(Context.INITIAL_CONTEXT_FACTORY,
                      "com.sun.jndi.ldap.LdapCtxFactory");
              properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
              try {
                  DirContext context = new InitialDirContext(properties);
                  Attributes attrs = context.getAttributes("dc=example,dc=com");
                  System.out.println("ALL Data: " + attrs.toString());
              } catch (NamingException e) {
                  e.printStackTrace();
              }
          }
          public static void main(String[] args) {
              DirectorySample sample = new DirectorySample();
              sample.doLookup();
          }
  
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/KPMRk.jpg" alt="enter image description here"><br>
  I want to show all users and roles list, so i need to change query or something else
  Thanks in Advance.</p>