Introduction to JNDI in Java
JNDI stands for Java Naming & Directory Interface. It is the product of Sun Microsystems Ltd. JNDI is an application programming interface (API) that is useful for developers to create applications. JNDI enables developers to provide different naming & directory services. The applications that are developed in Java, use JNDI for naming & directory service. JNDI works with the servers to fetch files from the database using naming conventions. It can also use in developing web applications. JNDI helps to locate the distributed components of applications. Without finding these distributed component applications can’t work. In this article, we discuss What is JNDI in Java?
JNDI offers naming & directory functionality to applications written using Java. It is independent of directory service implementation. It provides a common interface to all naming services. The main purpose of the naming service is to assign a name to an object and provide a way to access that object. Naming services keep the information in a central place so everyone can use it.
Advantages of JNDI in JAVA
Below are the Advantages of JNDI:
- You can develop portable code for applications using Data Source registered with JNDI.
- Using JNDI it is easy to maintain and edit the code.
- The most useful advantage of JNDI is connection pooling which means connections can reuse rather than creating new connections.
- JNDI enables the use of password encryption.
Architecture of JNDI
JNDI application consists of both application programming interface (API) and service provider interface (SPI). JNDI API allows java to get access to different naming & directory services.
Levels of JNDI Architecture:
Some points about the JNDI architecture:
- In this table, you can see the levels of the architecture.
- All levels of the architecture are connected to the java application.
- API provides a mechanism to bind an object to a name and to access naming & directory services.
- The SPI allows naming & directory services to be plugged in transparently.
- The last level shows the directories that are connected to the interface.
Directories SPI
Below are the Directories Supported by the SPI:
Directory Name | Description |
Lightweight Directory Access Protocol (LDAP) | This protocol helps to manage, maintain, and retrieve the distributed information about the directory over the internet. |
Domain Name System (DNS) | It is a hierarchical naming system that helps to find the website for internet users. |
Network Information Service (NIS) | This protocol allows you to maintain data such as user and hostname. |
Common Object Request Broker Architecture (CORBA) | This protocol provides a platform for distributed objects to communicate with one another. |
Remote Method Invocation (RMI) | This API allows an object of one machine to access an object on another machine. |
Packages of JNDI
Below are the Packages of JNDI with Description:
Packages | Description |
javax.naming | This package provides classes & interfaces which are used to access the naming services. |
javax.naming.directory | This package supports the functionality of the directory service. It extends java.naming package. |
javax.naming.event | This package provides the package & interfaces for the notification of events in naming & directory services. |
javax.naming.ldap | This package provides classes & interfaces that support specific features of the LDAP v3. |
javax.naming.spi | This package is useful only for system programmers. Provide support for multiple naming systems, java objects, plug-in architecture. |
To use JNDI you must have a service provider. JNDI package includes classes & interfaces below table shows classes regarding the packages.
Packages | Classes Regarding the Packages |
javax.naming | Context, InvalidNameException, LimitExceededException, LinkException, Name, NameClassPair, NameParser, NamingEnumeration, NamingException, NamingSecurityException, RefAddr, Reference. |
javax.naming.directory | Binding, Context, InitialContext, Name, NameClassPair, NamingEnumeration, NamingException. |
javax.naming.event | Binding, Context, Name, NamingException |
javax.naming.ldap | Context, InitialContext, NamingException, ReferralException. |
javax.naming.spi | CannotProceedException, Context, Name, NamingException. |
Example to Implement JNDI in Java
Below is the basic program to access attributes in JNDI:
Code:
import javax.naming. *;
import javax.naming.directory.*;
class RootDSE
{
public static void main (String args[])
{
DirContext ctx =new InitialDirContext();
Attributes attrs = ctx. getAttributes( “cn = john, ou= people”);
System.out.println(“ name is “ attrs.get(“nm”).get());
}
}
Output:
Conclusion
JNDI is an application programming interface provided by java. By using JNDI you can save and retrieve distribute objects over the network. It provides functionality for naming the object and directory operations. It is useful to develop more portable applications.
Recommended Articles
This is a guide to What is JNDI in Java?. Here we discuss the introduction, Advantages, Packages, Architecture and Directories Supported by the SPI. You may also have a look at the following articles to learn more –