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

LDAP Connection

Jamie Smithy

Joined: Aug 15, 2005
Posts: 1
I am trying to get a swing application to connect to an ldap server using a url that is typed into a text field. i can't work out the correct syntax and as a result am getting a variety of errors. The code for the part of the program that tries to connect to the LDAP server is shown below. If anyone could possibly help it would be greatly appreciated.

private void connectAndDisplayJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectAndDisplayJButtonActionPerformed

this.displayJTextArea.setText("Test Text");
//TODO: Add code here

urlString = ldapUrlJTextField.getText();

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, urlString);

try {

System.out.println("Creating Initial Context...");
DirContext ctx = new InitialDirContext(env);

System.out.println("Building Search Criteria...");

Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("on"));

System.out.println("Searchingdirectory ...");
NamingEnumeration answer ="", matchAttrs);

//SearchControls searchControls = new SearchControls();
//String filter = "cn=*";
//System.out.println("Searching Directory ...");
//NamingEnumeration answer ="",filter,searchControls);


System.out.println("\nClosing the context...\n");
}catch (NamingException e) {
System.err.println("Problem getting attribute: "+ e);

public void printSearchEnumeration(NamingEnumeration answer){

while (answer.hasMoreElements()){

SearchResult sr=(SearchResult);
this.displayJTextArea.setText(displayJTextArea.getText() + "\n"+sr.getName());

if(sr.getAttributes() == null){

System.out.println("No attributes");
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "No attributes");

NamingEnumeration ae = sr.getAttributes().getAll();
while (ae.hasMore()){
Attribute attr = (Attribute);
System.out.println("Attributes: " + attr.getID());
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "Attribute: " + attr.getID());

NamingEnumeration e = attr.getAll();
while (e.hasMore()){
System.out.println("value: " +;
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "value: " +;

}catch (NamingException e){

Harish Madhavan

Joined: Dec 10, 2003
Posts: 17
Can you post the stacktrace what you are getting ? Suggest to try LDAP code snippet in a seperate java program.Once unit tested, you can plug it in your Swing client.

Baiju Varugese
Ranch Hand

Joined: Dec 16, 2003
Posts: 74
I dont see any problem with the code u posted, try the following

1. trim the text which u got from the test field
2. some ldap may not allow the ananomous user to do search operations, so use security principal, credentials, to connect the ldap


Measure twice cut once carpenter theory <br /> <br />Baiju <br />SCJCP, SCBCD, IBM-486, IBM-484, SCEA Part I
I agree. Here's the link:
subject: LDAP Connection
It's not a secret anymore!