MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JNDI: Java Naming and Directory Interface

In this article we will discuss about JNDI , its overview, usage and also an example.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction:

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.

Description:

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.

Overview:

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.

Importance:

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.

Screenshots:

This screen shows that the application is running in the localhost and the port is 8084 respectively

Figure 1: This screen shows that the application is running in the localhost and the port is 8084 respectively.

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.

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"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Java Naming and Directory Interface (JNDI)</title>
    </head>
    <body>
        <h1>Welcome to the JNDI Application</h1>
    </body>
</html>

Here, this is just a normal JSP welcome page.

Listing 2 discovering_resources.java

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet discovering_resources</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet discovering_resources at " + request.getContextPath() + "</h1>");
            Context start_context, end_context;
            String name=null;
            try{
            start_context=new InitialContext();
            end_context=(Context)start_context.lookup("java:com/getpathoffiles");
            DataSource data_s=(DataSource)end_context.lookup("jdbc/Management");
            Connection conec=data_s.getConnection();
            Statement stmt=conec.createStatement();
            String quer="select St_Name from Student";
            ResultSet resset=stmt.executeQuery(quer);
            while(resset.next()){
                name=resset.getString("St_Name");
                System.out.println(name);
            }
            conec.close();            
            }catch(Exception e){
                e.getMessage();
            }

Here,

  • 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">
    <servlet>
        <servlet-name>discovering_resources</servlet-name>
        <servlet-class>jndipackage.discovering_resources</servlet-class>
        <init-param>
            <param-name>Name</param-name>
            <param-value>Value</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>discovering_resources</servlet-name>
        <url-pattern>/discovering_resources</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description> Embedded_Database_in_Netbeans </description>
        <res-ref-name>jdbc/Management</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

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.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login