Previous | Next | Trail Map | Java Objects and the Directory | Reading Objects from the Directory

Searches

The Search (in the Basics trail) lesson showed you how to use the various DirContext.search()(in the API reference documentation) methods. When you perform a search using a SearchControls(in the API reference documentation) parameter, you can control what gets returned in each SearchResult(in the API reference documentation). Specifically, if you invoke the SearchControls.setReturningObjFlag()(in the API reference documentation) method with true, and pass that to search(), each entry in the SearchResult will contain the corresponding Java object. This example searches the context that we used to bind the various objects in the Storing Objects (in the Java Objects and the Directory trail) lesson, and prints each result's object (class name and object reference):
SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);

// Specify the search filter to match any object
String filter = "(objectclass=*)";

// Search for objects using filter
NamingEnumeration answer = ctx.search("", filter, ctls);
The output of this example is as follows:
#java Search
ou=Groups: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@1dacd78d
ou=People: com.sun.jndi.ldap.LdapCtx: com.sun.jndi.ldap.LdapCtx@1dacd91b
cn=Button: java.awt.Button: java.awt.Button[button0,0,0,0x0,invalid,label=Push me]
cn=favorite: Fruit: orange
cn=favDrink: Drink: water

Reading Objects from the Directory: End of Lesson

What's next? Now you can:


Previous | Next | Trail Map | Java Objects and the Directory | Reading Objects from the Directory