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 ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.