The JNDI is an Application Programming Interface (API) which provides the naming and directory functionality to Java applications and these applications can access the naming and directory services by using this API. It also contains a service provider interface (SPI) that enables a variety of naming and directory services in order to allow applications written in Java to use the JNDI API and access the services respectively. The components usually access other components and resources including the database system depending on the need in order to access and use a distributed application. The naming service of JNDI enables the components in order to find the location of related components and resources that are required. JNDI has made it possible to find the components of an application and the related components of other applications. An API is required to make the providers connected in order to access the different naming and directory services. The applications programmed in Java are integrated into different environments in an easy manner by using this JNDI API. JNDI works with the versions of java including J2SE as well as J2EE. The developers can create the applications which can run on any platform and this has been made possible by JNDI. The applications developed are then enabled in order to be used for many different naming and directory services. The JNDI API provides different means in order to check about the object and looking at it by indicating the location and is used as the initialization point.
The JNDI API is used to see the objects in a network and is used by Remote Method Invocation (RMI). This API provides a context which shows that where there is a need to look for a specific object in order to find out about the related work by it. JNDI contains a set of interfaces and it is not a service like JDBC and it also allows the java applications using an API in order to access many different directory service providers. The API of JNDI basically helps to provide the special process using which an object gets bound with name, an interface in a directory in order to allow the queries to be executed, an event interface which helps the clients in order to determine the changed entries of a directory. The SPI present in JNDI provides the different services for directory and it includes Remote Method Invocation (RMI), Domain Name System (DNS), Novell NDS, File System, Network Information System (NIS), Lightweight Directory Access Protocol (LDAP) and Common Object Request Broker Architecture (CORBA) Common Object Services (COS) name service. As the JNDI API provides a programming interface in order to access the naming and directory services and it also helps to provide the functionalities of naming and directory which is usually used to search for different objects using the respective attributes of those objects and creating the association with objects of different attributes. The person is required to be familiar and know about the naming systems like surfing the web after entering the URL in the web browser. The naming service present in JNDI API helps to associate the names with objects in order to access those objects which are based on their names. The active directory service available in JNDI API also offers dynamic binding so that the J2SE applications can get the benefit from it. It is possible that the namespace of JNDI and the client is present at different JVMs. The provider of this API uses a lot of techniques in order to ensure that the retrieval of object at both ends is the correct one and to the point and it is the duty of provider to also ensure that an object moves from one JVM to another one and in a successful manner. The provider of JNDI is stored in every container of J2EE and the information of directory is basically stored in an internal database to know about this information later on and whenever the topic of JNDI is viewed in J2EE applications the container and supplier provider is usually mentioned and referred.
There are different packages available for programming using JNDI and these include javax.naming which is used to access the naming services, javax.naming.directory used in order to provide the basic functionality to access the directory services, javax.naming.event used in order to support the notifications of events for naming and directory services and javax.naming.spi is used as a service provide interface. JNDI is a generic interface and in order to access the above mentioned naming and directory service, there is a need to specify the service provider in order to use it. There is the hierarchy of class defined by the JNDI for the exceptions case which can be thrown at the time of performing the operations of naming and directory and the root class of this hierarchy is Naming-Exception. The J2EE applications use JNDI in order to store the information regarding configurations in a database system and this system is centralized.
The Java Development Kit available in the system consists of different JNDI interfaces. This directory service based API helps the clients in order to check and see the objects and data by using name. JNDI also helps in the storage of java objects and these objects can be of any type. It also helps in successful retrieval of the stored objects respectively. The architecture of JNDI containing different implementation possibilities helps in the support of multiple namespaces present in the SPI of JNDI. The Java 2 Platform, Enterprise Edition (J2EE) applications are combined together at one place using this JNDI API. The components can be easily added as well as updated in the existing complete system and it supports the dynamic application assembly and the applications to be deployed in a successful manner. The naming service present in this API acts as a central registry and helps in order to organize an enterprise application for components.
Figure 1: This screen shows that the application is running in the localhost and the port is 8084 respectively.
Figure 2:This screen shows the details when the application has executed successfully. In this case, the tomcat server is used. It lists all the important related tabs and the host is showing the same port as in the above screen that is 8084.
Implementation & Codes:
Listing 1: Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java Naming and Directory Interface (JNDI)</title>
<h1>Welcome to the JNDI Application</h1>
Here, this is just a normal JSP welcome page.
Listing 2 discovering_resources.java
out.println("<h1>Servlet discovering_resources at " + request.getContextPath() + "</h1>");
Context start_context, end_context;
String quer="select St_Name from Student";
- First we obtain the context path, which is the path where we will look for the resources.
- Now we use the obtained context to obtain the data source.
- Now we use this data source to make generic database connection and executing sql queries.
Listing 3: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<description> Embedded_Database_in_Netbeans </description>
Here, the above will be defining the xml file for the current web configuration. This will also define servlet mappings, welcome files ec.
Listing 4: Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/JNDI_Work"> </Context>
This will set antiJARLocking to true so that classloader will take extra measures to avoid JAR file locking when resources are accessed inside JARs through URL
Hope you liked it, see you next time.