Reputation: 37
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?
Upvotes: 1
Views: 6815
Reputation: 11134
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.
Upvotes: 0
Reputation: 691715
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?
Upvotes: 0
Reputation: 41858
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());
}
}
Upvotes: 3