In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the specific usage of JNDI. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The full name of JNDI (Java Naming and Directory Interface) is the java naming and directory interface. It is an application-designed API that provides developers with a common, unified interface to find and access various naming and directory services, similar to JDBC, which is built on an abstraction layer.
1. The concept and application of naming.
The Naming in JNDI is to bind (binding) the Java object to a container environment (Context) in the form of a name. Later, you can call the lookup method from the container environment to the JNDI container environment (Context) to find out the Java object bound by a name.
The advantage of this is that in real project applications, system programs or framework programs usually bind resource objects to the JNDI environment first, and later module programs running in the system or framework can find these resource objects from the JNDI environment. For example, when a Tomcat server starts, it can create a DataSource object that connects to a database system, and bind the DataSource object to the JNDI environment. Later, Servlet and JSP programs running in this Tomcat server can query the DataSource object from the JNDI environment for use, regardless of how the DataSource object is created. This method greatly enhances the maintainability of the system, so that when the connection parameters of the database system change, it is only a matter of concern to the Tomcat system administrator, and has nothing to do with all application developers.
The container environment (Context) is itself a Java object that can be bound to another container environment by a name. Binding one Context object to another Context object forms a parent-child cascade relationship, and multiple Context objects can eventually be cascaded into a tree structure, and several Java objects can be bound in each Context object in the tree.
Each box in the above figure represents the next Context object, and their binding names are a, b, c, d, e, respectively, where b and c are the child Context,d of a, the child Context of b and d. Each small ellipse in each box represents a Java object, and they all have a binding name, such as dog, pig, sheet, and so on. You cannot bind two Java objects with the same name in the same Context, and bind objects with the same name can appear in different Context. It can be seen that the cascading structure of the Context tree is very similar to the directory structure in the file system, and the relationship between Context and the bound Java objects in it is also very similar to the relationship between directories and files in the file system.
If you want to get a Context object, you can call its lookup method to get the java object bound to it. In addition, calling the lookup method of a Context object can also get any Context object in the Context tree, which only needs to specify the corresponding Context path in the lookup method.
There is no concept of "root" Context in JNDI, that is, JNDI operations do not start with a "root" Context object, but can start with any subsequent Context in the Context tree. Regardless of how a program must obtain a Context object as an operator before it can perform various JNDI naming operations, an InitialContext class is provided in JNDI API to create an entry Context object that is used as an JNDI naming operation.
Context is an interface, and the Context object is actually an instance object of an implementation class of Context. The process of selecting this specific Context implementation class and creating its instance object is accomplished by a Context factory class. The class name of this factory class can be specified by the environment property java.naming.factory.initial of JNDI, or it can be selected according to the Schema of the url parameter of the operation method of Context.
2. The concept and application of catalogue
Directory in JNDI is very different from the concept of Directory in file system. Directory in JNDI means that all attribute information of an object is saved in a container environment. The Directory principle of JNDI is very similar to that of JNDI command (Naming). The main difference is that the directory container environment stores the attribute information of the object, not the object itself, so the directory provides a variety of operations on attributes. In fact, the directory of JNDI (Directory) and naming (Naming) are often used together. The class provided in JNDI API that represents the directory container environment is a subclass of Context. Obviously, it can not only complete directory-related operations, but also complete all command (Naming) operations. DirContext is an extension of Context, which adds the operation function of directory attributes on the basis of Context, in which you can bind the attribute information of objects and find the attribute information of objects. The structure of the directory (Directory) in JNDI is as follows:
Each outer box in the figure above represents a DirContext object with the name of a, and b is the child DirContext of a. Each small ellipse in the figure represents a java object, and the boxes of each inner layer represent an object property. From the contents of the DirContext named a, we can see that in a DirContext container environment, you can bind both the object itself and the property information of the object. The bound object and the bound property are two completely independent things, even if their binding names are the same, their operations are completely independent. In addition, a property can have multiple property values. For example, the category property of a dog object has two property values set: meat and pet.
As you can see from the contents of the DirContext named b, it is also possible to bind only the property information of an object without binding any object itself in a DirContext container environment. Similar to the operation of Context, an InitialDirContext class is provided in JNDI API to create an entry DirContext object that is used as an JNDI naming and directory attribute operation.
3. Usage of JNDI: create a data source
What you do without JNDI
Connection conn = null;try {Class.forName ("com.mysql.jdbc.Driver", true,Thread.currentThread (). GetContextClassLoader ()); conn = DriverManager.getConnection ("jdbc:mysql://MyDBServer?user=xxx&password=xxx");. Conn.close ();} catch (Exception e) {e.printStackTrace ();} finally {if (connexual null) {try {conn.close ();} catch (SQLException e) {}
This approach is only applicable to small-scale development, and there will be many problems in large-scale development, such as:
The database server name MyDBServer, username, and password may all need to be changed, which causes the JDBC URL to be modified.
The database may be changed to another product, causing the JDBC driver package and class name to be modified.
With the increase of the actual use of terminals, the originally configured connection pool parameters may need to be adjusted.
In the development process, programmers should not need to care about "what is the specific database background? what is the JDBC driver?" For these problems, programmers should write programs without references to JDBC drivers, server names, user names, etc., but leave these problems to the container to configure and manage, so that programmers only need to reference these configurations and management.
Use JNDI
Context.xml configuration database connection information:
WEB-INF/web.xml ${catalina.base} / conf/web.xml
Introduce data sources into web.xml:
Index.html jdbc/mysql javax.sql.DataSource Container
Get the Connection object and query the database:
Import javax.naming.Context;import javax.naming.InitialContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet @ WebServlet ("/ test") public class Servlet1 extends HttpServlet {@ Override protected void service (HttpServletRequest req, HttpServletResponse resp) throws IOException {try {Context initContext = new InitialContext (); Context envContext = (Context) initContext.lookup ("java:/comp/env"); DataSource ds = (DataSource) envContext.lookup ("jdbc/mysql"); Connection conn = ds.getConnection () PreparedStatement ps = conn.prepareStatement ("select * from t_role"); ResultSet rs = ps.executeQuery (); System.out.println (rs.next ()); rs.close (); conn.close ();} catch (Exception e) {e.printStackTrace () } the above is the specific usage of JNDI shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.