• Post Reply Bookmark Topic Watch Topic
  • New Topic

LDAP Connection  RSS feed

Jamie Smithy
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = ctx.search("", matchAttrs);

//SearchControls searchControls = new SearchControls();
//String filter = "cn=*";
//System.out.println("Searching Directory ...");
//NamingEnumeration answer = ctx.search("",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)answer.next();
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)ae.next();
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: " + e.next());
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "value: " + e.next());

}catch (NamingException e){

Harish Madhavan
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!