File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes LDAP sample program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "LDAP sample program" Watch "LDAP sample program" New topic

LDAP sample program

Kevin Knowles
Ranch Hand

Joined: Dec 29, 2002
Posts: 45
Can anyone provide a simple program demonstrating
the use of LDAP?
Clayton Donley

Joined: Mar 03, 2003
Posts: 27
Here is an example using JNDI (included with Java 1.3 and above and available for download with older versions of Java). I've thrown in some comments that walk through what it does a bit.
This program does a simple LDAP search for the root entry in the directory. It doesn't print anything fancy, but shows the general process of searching and retrieving results.

(edited by Cindy to format code)
[ March 21, 2003: Message edited by: Cindy Glass ]

Clayton Donley, CTO<br />Octet String, Inc.<br />Phone: +1-847-358-9358 ext. 111<br />Email:<p>Author: LDAP Programming, Management, and Integration<br />Manning: <a href="" target="_blank" rel="nofollow"></a><br />Amazon: <a href="" target="_blank" rel="nofollow"></a>
Andee Weir

Joined: Sep 13, 2002
Posts: 12
Sun provide quite a good starter tutorial at Sun's JNDI tutorials
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
package security;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
public class JDSAuthentication {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://yoda:391";
public static String MY_SEARCHBASE = "dc=isdintegration,dc=com";
public static String MY_FILTER = "jabberID=test1@yoda";
public static String MGR_DN = "cn=directory manager";
public static String MGR_PW = "passwordadmin";
public static void main(String[] args) {
Hashtable env = new Hashtable();
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results =,MY_FILTER,constraints);
//now stop through the search results
while(results != null && results.hasMore()){
SearchResult sr = (SearchResult);
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);
String attrID = attr.getID();
System.out.println(attrID+" :");
for(Enumeration vals = attr.getAll();vals.hasMoreElements() {
}catch(Exception ex){
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
With which LDAP Servers do the above samples work?

DirContext.INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory".

Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another?

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another


Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
I am using Weblogic 8.1, trying to query/update the LDAP attributes for a user stored in the Weblogic's Embedded LDAP database.

I am able to query the LDAP database, if the used INITIAL_CONTEXT_FACTORY is com.sun.jndi.ldap.LdapCtxFactory(part of JDK1.4.1, I believe).

When I use Weblogic's classes,weblogic.jndi.WLInitialContextFactory
or weblogic.jndi.T3InitialContextFactory , I am getting the following exception.

javax.naming.NotContextException: Not an instance of DirContext

Hence my question, was, can com.sun.jndi.ldap.LdapCtxFactory JNDI driver be used for accesing most of the LDAP databases?
I agree. Here's the link:
subject: LDAP sample program
It's not a secret anymore!