Home > Java > javaTutorial > What is JNDI in Java?

What is JNDI in Java?

WBOY
Release: 2024-08-30 15:06:42
Original
887 people have browsed it

Java Naming and Directory Interface is the name of the interface in the Java programming language. It is an API( Application Program Interface) that works with servers and can fetch files from a database using naming conventions. The naming convention can be a single phrase or a word. It can also be incorporated in a socket to implement socket programming using servers transferring data files or flat files in a project. It can also be used in web pages in browsers where there are instances of many directories. JNDI provides users in Java the facility to search objects in Java using the Java coding language.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

The architecture of JNDI in Java

In the architecture, we notice the different directories associated with JNDI, which consists of an API and an interface known as Service Provider Interface(SPI).

What is JNDI in Java?

In this diagram, we notice the JNDI architecture, which is connected to the Java application. The levels are clearly mentioned that the JNDI API is above the interface, and the interface is used to connect to a lot of directories. Some of the directory services are mentioned below.

  • Lightweight Directory Access Protocol
  • Domain name service.
  • Java Remote Method Invocation.

The above mentioned are directories that JNDI SPI integrates with and builds a platform with JNDI implementation possibilities.

JNDI Packages

There are namely five packages in  Java using JNDI SPI. Some of the packages are javax.naming. The javax.naming is a package where it contains classes and interfaces for accessing naming services. There are functions like lookup, list Bindings, Name. The second one is the java.naming.directory. This package helps in getting the data as objects and is an advanced version of java.naming directory. There are also other packages that are java. naming. event and java. naming. spi.

Also, JNDI plays a major role in three of the latest Java technologies. They are:-

  • JDBC (The Java Database Connectivity package
  • JMS (The Java Messaging Service)
  • EJB( Enterprise Java Beans)

JDBC is for database processing which JMS is the messaging service app. EJB runs with Netbeans and Eclipse platform for running Java programs. The packages are present along with the technologies in which they are used.

JNDI is also used with the LDAP service provider. There are a series of codes that run the programming application in Java language.

There is a bind() and look up() in Java programming language and is used in naming an object and looking up an object from the directory.

Context.bind("name", object)
Copy after login

Here the name can be assigned any name to the current object in the directory. This is an example of the bind function where the object’s name is set.

Object hello= Context.lookup("name")
Copy after login

In this function, the object hello looks for the name of the object in the directory. There are also variations of serialized or non serialized data used as the kind of directory supports.

JNDI and its applications are used widely in the data analytics industry, where there is a lot of data that has to be mined, and there is a certain aspect of data being stored in different directories and files stored on different folders. It has wide use in the telecommunication industry where there is a calculation of bills taking place according to the hourly rate of conversation someone has.

Example of JNDI in Java

This code is a menu-driven program that asks the user to enter the Principal amount, and then it prints the Simple Interest, Compound Interest and the difference between the Simple and Compound Interest according to the user’s needs. The program also exits when the user doesn’t want to continue with the program further.   The rate of interest is fixed at 8.5% and the number of years taken for the interest to get generated is 7 years. Accordingly, all the interest rates are calculated.

To create a menu-driven program to enter the principal amount and calculate the simple interest, compound interest and absolute difference between them.

Code:

import java.io.*;
class Assignment1
{
public static void main(String[] args) throws Exception
{
BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Principal Amount : ");//prompt for entering the principal amount
float P = Float.parseFloat(ob.readLine());//accepting the principal amount
int choice = 0;
do{
choice = 0;// reseting the user's choice
//displaying the Menu of Options
System.out.println("------------- M E N U ----------------");
System.out.println("1 - To Find the Simple Interest");
System.out.println("2 - To Find the Compound Interest");
System.out.println("3 - To Find the Difference between the Simple and Compound Interests");
System.out.println("4 - To Exit The Program");
System.out.print("Enter Choice : ");//prompting for user's choice
choice = Integer.parseInt(ob.readLine());//accepting user's choice
System.out.println("");// line feed between menu and result
switch(choice)
{
case 1://for simple interest
System.out.println("The Simple Interest is Rs."+simple(P));
break;
case 2://for compound interset
System.out.println("The Compound Interest is Rs."+compound(P));
break;
case 3://for difference between simple and compound interests
System.out.println("The Absolute Difference is Rs."+(compound(P)-simple(P)));
break;
case 4:
System.out.println("Program Terminated");
break;
default://for a wrong choice entered by the user
System.out.println("Invalid Option");
}//end of switch(choice)
System.out.println("\n");//linefeed between two consecutive choices by the user
}while(choice!=4);//end of do-while
}//end of main
public static float simple(float p)//to calculate the simple interest
{
return (float)((p*8.5*7.0)/100.0); //returning the simple interest
}//end of simple
public static float compound(float p)//to calculate the compound interest
{
return (p*(float)(Math.pow((1.0+(8.5/100.0)),7.0)-1.0));//returning the compound interest
}//end of compound
}//end of class
Copy after login

Output:

What is JNDI in Java?

Here we enter the Principal amount of Rs 10000, and we find out the simple and the compound interest as well as the difference.

Conclusion

This article sees the programming concept of a Java program and its application in the BlueJ platform. The code is used to calculate the interest rate from the principal. It returns the simple interest, compound interest, and exits if the user wishes to. Also, we see how JNDI is used in directories and servers, the packages used in programming and finding and searching directory using objects. The main use of JNDI is whenever there is a directory associated with it, and it has to be searched for meaningful insights about the data. This concept is particularly unique in Java, and it’s not generally seen in other programming languages like C, C++, and Python.

The above is the detailed content of What is JNDI in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template