The Java Naming and Directory Interface (JNDI) allows the Java programs to interact with any naming service and directory service that implements JNDI. The most used operation of JNDI is the search operation. The search operation contains a connection, the base to start the search from, the scope of the search, and a search filter. The search filter is like a SQL query in which a user can tell the criteria for search. Searches include attribute name and a value to be looked up. Filter can use Boolean logic and wildcards. The JNDI can perform the following types of searches:
- Context.lookup(): It returns a stored object corresponding to the DN in the server.
- Context.list(): It returns the names bound and the class names of the objects bound in the context. It is used for browser-style applications that just want to display the names of objects in a context. It returns an enumeration of NameClassPair. Each NameClassPair consists of the object’s name and its class name. The following code fragment lists the contents of the “Home22=People” directory (i.e., the files and directories found in “Home22=People” directory).
NamingEnumeration list = ctx.list(”Home22=People”);while (list.hasMore()) {
NameClassPair List22 = (NameClassPair)list.next();
System.out.println(List22);
} - Context.listBindings(): It returns the names bound and the objects bound in the context. It is intended for applications that need to perform operations on the objects in a context. It returns an enumeration of Binding. Binding is a subclass of NameClassPair. A binding contains not only the object’s name and class name, but also the object. The following code enumerates the “Home22=People” context, printing out each binding’s name and object.
NamingEnumeration bindings_home = ctx.listBindings(”Home22=People”);while (bindings_home.hasMore()) {
Binding Bdhom22 = (Binding)bindings_home.next();
System.out.println(Bdhom22.getName() + “: ” + Bdhom22.getObject());
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.