Of the 13 J2EE specifications (1) JNDI, j2ee13 specification jndi
Recently I was studying J2EE. I thought J2EE was also a high-level language similar to J2SE, c #, and vb. I didn't expect that when I learned the video, I realized that other languages are not like this.
J2EE is a set of technical architecture completely different from traditional application development. It contains many components, which can simplify and standardize the development and deployment of application systems, thereby enhancing portability, security, and reuse value. In short, it is a set of standards!
The difference between the J2EE component and the "standard" Java class is that it is assembled in a J2EE application and has a fixed format and complies with the J2EE specifications, and is managed by the J2EE server. The J2EE specification defines J2EE components as follows: the client application and applet are components running on the client; Java Servlet and Java Server Pages (JSP) are Web components running on the Server; the EnterpriseJava Bean (e jb) component is 13 components (specifications) running on the server ).
Today, let's talk about its JNDI component.
I. What is JNDI?
JNDI-Java Naming and Directory Interface (Java Naming and Directory Interface) is a group of APIs that access Naming and Directory services in Java applications.
Naming Service, to put it bluntly, provides management of a name-Key-Value pair, that is, a Key-Value pair. A Key represents the name of a resource, and a Value represents the real address of the resource, the naming service allows you to find the corresponding object or resource by a unique name. In this way, the program only needs to know the name of a resource and can access it through JNDI, without the real physical address of the resource. This is a bit like a DNS service. DNS resolves a domain name to an IP address, so that you only need to enter a unique website name (that is, a domain name) in your browser to access the website, you do not need to remember the real IP address of the website.
Directory Service also provides a public resource management service. Directory Service is a special type of database that organizes and stores various public resources according to a certain data structure, such as a tree structure. The difference between such a special database and a traditional relational database is that it optimizes the query, and its data structure allows you to quickly find the desired resources, which guarantees a fast search capability, however, this design also sacrifices the efficiency of other aspects, for example, its update efficiency is much lower.
The key-value pairs managed in the directory service are also named. However, the key-value pairs have hierarchies, such as a tree, that is, a name or a name with a hierarchy, you can locate a subtree, not just an attribute. It can be seen that the directory service extends the concept of the Naming Service to provide a hierarchical information library. A directory service usually has a naming service, but a Naming Service does not have to have a directory service.
To understand the role of JNDI, we can refer to "What should we do without JNDI? After using JNDI, what will we do ?" This issue is discussed.
Without JNDI: When a programmer develops an application that needs to be developed to access the MySQL database, the programmer encodes a reference to the MySQL JDBC Driver Class, connect to the database by using the appropriate jdbc url.
Like the following code:
<Span style = "font-size: 14px;"> Connection conn = null; try {Class. forName ("com. mysql. jdbc. driver ", true, Thread. currentThread (). getContextClassLoader (); conn = DriverManager. getConnection ("jdbc: mysql: // MyDBServer? User = qingfeng & password = mingyue ");/* use conn and perform SQL operations */...... conn. close ();} catch (Exception e) {e. printStackTrace ();} finally {if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {}}</span>
This is a traditional practice and is also a common practice for non-Java programmers (such as Delphi and VB. In general, this method will not cause problems in small-scale development. As long as programmers are familiar with the Java language, understand the JDBC technology and MySQL, they can quickly develop corresponding applications. Let's take a look at the issues.
1. The database server name MyDBServer, user name, and password may need to be changed, resulting in the jdbc url needing to be modified;
2. The database may use another product. For example, if you use DB2 or Oracle, The JDBC driver package and class name must be modified;
3. As the number of terminals in use increases, the original connection pool parameters may need to be adjusted;
4 ,......
Solution:
Programmers do not need to worry about "what is the specific database background? What is the JDBC driver? What is the jdbc url format? What is the user name and password used to access the database ?" And so on, the program compiled by the programmer should not reference the JDBC driver, no server name, no user name or password-or even no database pool or connection management. Instead, the problem is assigned to the J2EE container for configuration and management. The programmer only needs to reference the configuration and management.
As a result, JNDI is available.
After using JNDI:
First, configure the JNDI parameter in the J2EE container, define a data source, that is, the JDBC reference parameter, and set a name for the data source. Then, in the program, use the data source name to reference the data source to access the background database.
The procedure is as follows (using JBoss as an example ):
1. Configure the data source
Under the D:/jboss420GA/docs/examples/jca folder of JBoss, there are many data source definition templates referenced by different databases. Copy the mysql-ds.xml file to the server you are using, such as D:/jboss420GA/server/default/deploy. Modify the contents of the mysql-ds.xml file so that you can access your MySQL database correctly through JDBC, as shown below:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><datasources><local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/lw</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>rootpassword</password><exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata></local-tx-datasource></datasources></span>
Here, a data source named MySqlDS is defined. Its parameters include jdbc url, Driver Class Name, user name, and password.
2. reference the data source in the program:
<Span style = "font-size: 14px;"> Connection conn = null; try {Context ctx = new InitialContext (); Object datasourceRef = ctx. lookup ("java: MySqlDS"); // reference the data source DataSource ds = (Datasource) datasourceRef; conn = ds. getConnection ();/* use conn to perform database SQL operations */...... c. close ();} catch (Exception e) {e. printStackTrace ();} finally {if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {}}</span>
The amount of programming code for directly using JDBC or referencing a data source through JNDI is almost the same, but the current program does not need to care about specific JDBC parameters. After the system deployment, if the database parameters change, you only need to reconfigure the mysql-ds.xml to modify the JDBC parameters, as long as the data source name remains unchanged, then the program source code does not need to be modified.
It can be seen that JNDI avoids the tight coupling between the program and the database, making the application easier to configure and deploy.
What are the 13 standards of j2ee?
The J2EE platform consists of a complete set of Services, application interfaces (APIs), and Protocols. It provides Function Support for developing Web-based multi-layer applications, the following is a brief description of the 13 technical specifications in J2EE (limited by space ):
1. JDBC (Java Database Connectivity ):
JDBC APIs provide a unified way to access different databases. Like ODBC, JDBC shields developers from some details. In addition, JDCB's access to the database is also platform-independent.
2. JNDI (Java Name and Directory Interface ):
The jndi api is used to run the name and directory services. It provides a consistent model to access and operate enterprise-level resources such as DNS and LDAP, local file systems, or objects on application servers.
3. EJB (Enterprise JavaBean ):
One of the reasons why J2EE technology has won widespread attention is EJB. They provide a framework for developing and implementing distributed business logic, significantly simplifying the development of scalable and highly complex enterprise-level applications. The EJB specification defines when and how the EJB component interacts with their containers. Containers are responsible for providing public services, such as directory services, transaction management, security, resource buffer pools, and fault tolerance. However, it is worth noting that EJB is not the only way to implement J2EE. Due to the openness of J2EE, some vendors can achieve the same purpose in parallel with EJB.
4. RMI (Remote Method Invoke ):
As its name indicates, the RMI protocol calls methods on remote objects. It uses serialization to transmit data on the client and server. RMI is a more underlying protocol used by EJB.
5. Java IDL/CORBA:
With the support of Java IDL, developers can integrate Java and CORBA. They can create Java objects and expand them in corba orb, or they can also create Java classes and serve as the customers of the CORBA objects expanded with other ORB. The latter method provides another way through which Java can be used to integrate your new applications with old systems.
6. JSP (Java Server Pages ):
A JSP page consists of HTML code and Java code embedded in it. The server processes the Java code after the page is requested by the client, and then returns the generated HTML page to the browser of the client.
7. Java Servlet:
Servlet is a small Java program that extends the functions of Web servers. As a server-side application, it is executed when requested, which is similar to CGI Perl script. Most of the functions provided by Servlet are similar to those provided by JSP, but the implementation methods are different. JSP usually embeds a small amount of Java code in most HTML code, while servlets is all written in Java and generates HTML.
8. XML (Extensible Markup Language ):
XML is a language that can be used to define other Markup languages. It is used to share data in different business processes. The development of XML and Java are mutually independent. However, the same objective of XML and Java is platform independence. By combining Java and XML, you can get a perfect platform-independent solution.
9. JMS (Java Message Service ):
MS is an application interface (API) used to communicate with message-oriented middleware ). It supports point-to-point domains, publish/subscribe domains, and supports the following types: Approved message transmission and transactional message transmission, supports consistent messages and persistent subscribers. JMS also provides another method for your application and other full text>
What are the 13 technical points of J2EE? What can be done?
The J2EE platform consists of a complete set of Services, application interfaces (APIs), and protocols,
It provides functional support for developing Web-based multi-layer applications. It includes 13 core technical specifications:
(1) Java Database Connectivity (JDBC)
Access a variety of databases in a unified manner
(2) Java Naming and Directory Interface (JNDI)
It is used for name and directory services. It provides a consistent model to access and operate enterprise-level resources, such as DNS and LDAP, and local file systems.
(3) Enterprise Java Beans (EJB)
Provides a framework for developing and implementing distributed business logic, significantly simplifying the development of scalable and highly complex enterprise-level applications
(4) assumerver Pages (JSPs)
Used to create dynamic web pages
(5) Java servlets
Most of the functions provided are similar to those provided by JSP, but the implementation methods are different.
(6) Remote Method Invocation (RMI)
Call some methods on remote objects and use continuous sequence to transmit data on the client and server
(7) Interface Description Language (IDL)
Integrate Java and CORBA
(8) Java Transaction Architecture (JTA)
Various transactions can be accessed.
(9) Java Transaction Service (JTS)
Specifies the implementation method of the Transaction Manager.
(10) JavaMail
The API used to access the mail server. It provides a set of abstract classes of the mail server.
(11) JavaBeans Activation Framework (JAF)
JavaMmail uses JAF to process MIME-encoded email attachments. The MIME byte stream can be converted to a JAVA object or a JAVA object.
(12) Java Messaging Service (JMS)
Is an application interface (API) used to communicate with message-oriented middleware)
(13) Extensible Markup Language (XML)
XML is a language that can be used to define other Markup languages.