1

I am attempting to query a LDAP server to return a Directory Entry based on one of 4 ID's submitted by the user. I created an Info object to store the LDAP data, but how would I retrieve the data then output it back to the user in a formatted table?

cbj
  • 37
  • 2
  • 8

3 Answers3

3

You should use JNDI to do the querying, and a simple tutorial is at:

http://www.stonemind.net/blog/2008/01/23/a-simple-ldap-query-program-in-java/

But here is the main part that should help you:

        String url = "ldap://directory.cornell.edu/o=Cornell%20University,c=US";
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url);
        DirContext context = new InitialDirContext(env);

        SearchControls ctrl = new SearchControls();
        ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration enumeration = context.search("", query, ctrl);
        while (enumeration.hasMore()) {
            SearchResult result = (SearchResult) enumeration.next();
            Attributes attribs = result.getAttributes();
            NamingEnumeration values = ((BasicAttribute) attribs.get(attribute)).getAll();
            while (values.hasMore()) {
              if (output.length() > 0) {
                output.append("|");
              }
              output.append(values.next().toString());
            }
        }
James Black
  • 41,583
  • 10
  • 86
  • 166
0

You need to use JNDI to query the LDAP server. Look at examples here. But please, don't do this in a JSP. This will need Java code, and JSPs shouldn't contain Java code. See How to avoid Java code in JSP files?

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Agree, you should not use Java in a JSP, that's bad form. Also, I would recommend the UnboundID LDAP SDK over JNDI, it's easier, faster, better, clearer.

Terry Gardner
  • 10,957
  • 2
  • 28
  • 38