Application development based on JNDI


JNDI (the Java Naming and Directory Interface) is a set of APIs for accessing naming and directory services in Java applications. Naming services associate names with objects so that we can access objects by name. Directory service is a naming service in which objects have not only names but also attributes.

Naming or directory services enable you to centrally store common information, which is important in network applications because it makes such applications more coordinated and easier to manage. For example, printer settings can be stored in a directory service for use by printer related applications.

This article provides a quick tutorial in the form of code examples so that you can start using JNDI. It:

1. Provides an overview of JNDI

2. Describe the characteristics of JNDI

3. Experienced the application development with JNDI

4. It shows how to use JNDI to access LDAP server, for example, sun one directory server

5. It shows how to use JNDI to access J2EE services

6. Sample code is provided, which you can adapt to your own application

JNDI overview

We all use naming services unconsciously every day. For example, when you enter a URL in a web browser, DNS (domain name system) converts the symbolic URL name into a communication identifier (IP address). Objects in the naming system can be names in DNS records, EJB components (Enterprise JavaBeans components) in application servers, and user profiles in LDAP (Lightweight Directory Access Protocol).

Directory services are natural extensions of naming services. The key difference between the two is that objects in the directory service can have attributes (for example, users have email addresses), while objects in the naming service have no attributes. Therefore, in the directory service, you can search objects according to attributes. JNDI allows you to access files in the file system, locate objects registered by remote RMI, access directory services such as LDAP, and locate EJB components on the network.

JNDI is a good choice for applications such as LDAP clients, application launchers, class browsers, network management utilities, and even address books.

JNDI architecture

The JNDI architecture provides a standard set of naming system independent APIs, which are built on the drivers related to the naming system. This layer helps to separate the application from the actual data source, so whether the application accesses LDAP, RMI, DNS, or other directory services. In other words, JNDI is independent of the specific implementation of directory service. As long as you have a directory service interface (or driver), you can use directory. As shown in Figure 1. Figure 1: JNDI architecture

An important point to note about JNDI is that it provides application programming interface (API) and service provider interface (SPI). The real meaning of this is that if you want your application to interact with a naming service or directory service, you must have a JNDI service provider for this service, which is where JNDI SPI works. Service providers are basically a set of classes that implement JNDI interfaces for various specific naming and directory services – much like JDBC drivers implement JDBC interfaces for various specific database systems. As an application developer, you don’t have to worry about JNDI SPI. You just need to make sure that every naming or directory service you want to use has a service provider.


JNDI is included in Java 2 SDK version 1.3 and above. There is also a standard extension for JDK 1.1 and 1.2. The latest version of Java 2 SDK 1.4. X includes several enhancements and the following naming / directory service providers:

1. LDAP (Lightweight Directory Access Protocol) service provider

2. CORBA cos (common object request broker architecture common object services) naming service provider

3. RMI (Java Remote Method Invocation) registration service provider l DNS (domain name system) service provider

More service providers

The list of service providers that can be downloaded can be found at the following website: Of particular interest may be the windows registry JNDI service provider provided at: This service provider allows you to access Windows XP / 2000 / NT / me / 9x S registry. You can also download JNDI / LDAP booster pack at the following website: This booster pack includes support and extensions for popular LDAP control. It replaces the booster pack bundled with the LDAP 1.2.1 service provider. More information about control and extension can be found on the following website: Another interesting service provider is sun’s service provider that supports DSML v2.0 (directory service markup language). The purpose of DSML is to build a bridge between directory service and XML.


The JNDI API consists of five packages:

1. Javax.naming: contains classes and interfaces for accessing naming services. For example, it defines the context interface, which is the entry for the naming service to execute the query.

2. an extension of the named package, which provides classes and interfaces for accessing directory services. For example, it adds new classes to attributes, provides dircontext interface representing directory context, and defines methods to check and update attributes of directory objects.

3. Javax.naming.event: supports time notification when accessing naming and directory services. For example, the namingevent class is defined to represent the events generated by the naming / directory service, and the naminglistener interface for listening to namingevents is defined.

4. Javax.naming.ldap: this package provides support for the operation and control of LDAP version 3 extensions. The general package does not contain these operations and controls.

5. Javax.naming.spi: this package provides a method to dynamically add support for accessing naming and directory services through javax.naming and related packages. This package is for developers interested in creating service providers.

JNDI context

As mentioned earlier, naming services associate names with objects. This connection is called binding. A set of such bindings is called context, which provides resolution (that is, the lookup operation of the returned object). Other operations include binding and unbinding names, and listing the names of the bindings. Note that the name of a context object can be bound to another context object with the same naming convention. This is called a sub context. For example, if the directory / home in UNIX is a context, then the subdirectory relative to this directory is the sub context – for example, guests in / home / guests is the sub context of home. In JNDI, the context is represented by the interface javax.naming.context, which is the key interface to interact with the naming service. In context (or discussed later)

Each naming method in dircontext) interface has two overloaded forms:

1. Lookup (string name): accepts string names

2. Lookup (javax. Naming. Name): accept the structure name, for example, compositename (name across multiple naming systems) or composdname (name in a single naming system); They all implement the name interface. An example of compound name is: CN = mydir, CN = q Mahmoud, Ou = people. An example of composite name is: CN = mydir, CN = q Mahmoud, Ou = people / myfiles / max.txt (here, myfiles / max.txt is the file name representing the second part). Javax.naming.initialcontext is a class that implements the context interface. Use this class as the entry for naming services. In order to create the initialcontext object, the constructor sets a set of properties in the form of Java. Util. Hashtable or its subclass (for example, properties). An example is given below:

  Hashtable env = new Hashtable(); // select a service provider factory env.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.fscontext.RefFSContext”); // create the initial context Context contxt = new InitialContext(env);

  INITIAL_ CONTEXT_ Factory specifies the name of the factory class in the JNDI service provider. Factory is responsible for creating the appropriate initialcontext object for its service. In the code snippet above, the factory class is specified for the file system service provider. Table 1 shows the factory classes of the supported service providers. Note that the factory classes of file system service providers need to be downloaded separately from sun, and J2SE 1.4. X does not contain these classes.

Table 1: context initial_ CONTEXT_ Value of factory

  Name Service Provider Factory File System com.sun.jndi.fscontext.RefFSContextFactory LDAP com.sun.jndi.ldap.LdapCtxFactory RMI com.sun.jndi.rmi.registry.RegistryContextFactory CORBA com.sun.jndi.cosnaming.CNCtxFactory DNS com.sun.jndi.dns.DnsContextFactory

In order to obtain or resolve an object from a naming service or directory by name, use the lookup method of context: object obj = contxt. Lookup (name). The lookup method returns an object that represents the son of the context you want to find.

Recommended Today

SSH account and password free login

What is SSH SSH is a security protocol created on the basis of application layer and transport layer, which provides a secure transmission and use environment for shell (shell) on the computer. For the author who is not good at server operation, it is a tool that can log in to the server remotely How […]