Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How tp use SearchFilter with JNDI

raga menon
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i have to search the entry and display the enties. here i used the search
filter for that, when i try to compile the program am getting the error.


import javax.naming.*;

import java.util.Hashtable;

* Demonstrates how to perform a search by specifying a search filter
* and default search controls. Functionally identical to
* usage: java SearchWithFilterRetAll
class SearchWithFilter
public static void main(String[] args) {

// Set up the environment for creating the initial context
Hashtable env = new Hashtable(11);
env.put(Context.PROVIDER_URL, "ldap://localhost:2389/dc=example,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "secret");

try {
// Create initial context
DirContext ctx = new InitialDirContext(env);

// Create default search controls
SearchControls ctls = new SearchControls();

// Specify the search filter to match
// Ask for objects with attribute sn == Geisel and which have
// the "mail" attribute.
String filter = "(uid=user.10)";

// Search for objects using filter
NamingEnumeration answer ="ou=People", filter, ctls);

// Print the answer

// Close the context when we're done
} catch (Exception e) {

how could i achive this..

Oliver Kamps
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raga,

code is much easier to read if you put it into code tags (see the buttons below the edit box).

You could have saved some time by providing the error message in your question. My compiler says:

"search cannot be resolved"

You have not declared the variable "search" anywhere in your code.

Hope this helps,
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic