O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Searching a Directory

One of the most useful features a directory service can offer is the ability to search its entries for attribute values that meet certain criteria. JNDI supports this kind of searching in directory systems, which means you can implement search functionality in your JNDI applications. DirContext provides different search() methods that allow you to specify what you are searching for and control how the search operates.

Search Criteria

You can specify what you are searching for in two ways. The simpler technique is to create a set of attributes that serves as the search criteria . In this case, you can set an attribute value, meaning that an entry must have that attribute value to match, or leave the value empty so that all entries that have the attribute match no matter what the value.

The more flexible way to specify search criteria is with a search filter string. A search filter allows you to express search criteria using LDAP search syntax, specified in RFC 2254. Note that this syntax works with all JNDI providers, not just LDAP; it’s the JNDI standard for searching all kinds of directories . The search filter is a String that takes the following general form:

(attribute operator value)

You can use an asterisk (*) to represent a wildcard. For example, here’s how to search for all entries in an LDAP directory:

(objectclass=*)

A search for all users takes the form of:

(objectclass=user)

You can also use the wildcard character to represent completion, just as in ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required