I'm starting studies on enterprise applications, and according to my teacher I'm going to need this java technology and would like to know what JNDI is exactly. If possible I wanted an example of its use.
I'm starting studies on enterprise applications, and according to my teacher I'm going to need this java technology and would like to know what JNDI is exactly. If possible I wanted an example of its use.
What is it?
JNDI is an API used in applications that access external resources, it allows you to get these resources through the name. It specifies the SPI service interface and this mechanism allows the support of various directory services, such as: LDAP, DNS, NIS, RMI, CORBA, among others. The application that uses JNDI looks for the resources through a method called lookup.Esta API has a configuration environment, and through it we can pass several information, these should be informed in the creation of a context, it can be by a file.properties or a HashTable.
If a HashTable is used we can insert the following parameters:
Context.INITIAL_CONTEXT_FACTORY → Used to inform the class responsible for creating the InitialContext. Usually it depends on the application server used in the application; Context.PROVIDER_URL → Used to enter the URL of the provider. It also depends on the application server used;
If it is a service with authentication, we use the following parameters to inform them to the configuration environment:
Context.SECURITY_PRINCIPAL → To inform the user; Context.SECURITY_CREDENTIALS → To enter the password;
If a properties file is used we must define the names:
java.naming.factory.initial → Used to inform the class responsible for creating InitialContext. Usually it depends on the application server used in the application; java.naming.provider.url → Used to enter the URL of the provider; java.naming.security.principal → To inform the user; java.naming.security.credentials → To enter the password;
The names placed in the properties file are the same as those represented by the attributes of the context or InitialContext class. The parameters shown above are the most used, but we have several others that can be found in the documentation of class javax.naming.Context; It is possible to define a class that creates the InitialContext instead of using a class provided by an application server, for this we must extend the InitialDirContext class and also have an interface that implements the DirContext interface. The resources are stored in an area that can be called a namespace, and we can search these resources for use in our application, through the lookup. There are 3 types of namespace:
java: global It is the portable way of searching an EJB remote using JNDI lookup. It follows the following pattern: java: global