Java was first launched on browsers and client machines. At that time, many people questioned whether it was suitable for server-side development. Now, with the increase in third-party support for the Java2 Platform Enterprise Edition (J2EE), Java is widely accepted as one of the preferred platforms for developing enterprise-level server solutions.
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.
In this article, I will explain 13 core technologies supporting J2EE: JDBC, JNDI, ejbs, RMI, JSP, Java Servlets, XML, JMS, Java IDL, JTs, JTA, javamail and JAF will also describe when and where to use these technologies. Of course, I would also like to introduce how these different technologies interact.
In addition, to make you better feel the real applications of J2EE, I will introduce these technologies in the WebLogic application server, a widely used product environment from BEA Systems. No matter whether you are new to WebLogic application server and J2EE, or project managers and system analysts who want to know what benefits J2EE can bring, I believe this article will be of great reference value.
Macro impression: distributed architecture and J2EE
In the past, layer-2 applications-usually called client/server applications-were the most talked about. In many cases, the only service provided by the server is the database service. In this solution, the client program is responsible for data access, business logic implementation, display results with appropriate styles, pop-up preset user interfaces, and accept user input. The Client/Server structure is usually easier during the first deployment, but it is difficult to upgrade or improve. It is often based on a proprietary protocol, usually a database protocol. It makes it very difficult to reuse business logic and interface logic. More importantly, in the web era, L2 applications usually do not have good scalability, making it difficult to adapt to Internet requirements.
Part of Sun's design for J2EE is to solve the defects of the L2 structure. Therefore, J2EE defines a set of standards to simplify the development of layer-N enterprise applications. It defines a set of standardized components and provides complete services for these components. J2EE also automatically handles many implementation details for applications, such as security and multithreading.
Using J2EE to develop N-layer applications includes dividing different layers in a L2 structure into multiple layers. An N-tier application a can provide a separate layer for each of the following services:
Display: in a typical web application, the browser running on the client machine is responsible for implementing the user interface.
Dynamic generation and display: although the browser can display some dynamic content, to be compatible with different browsers, these dynamic generation tasks should be carried out on the Web server, using JSP, Servlets, or XML (Extensible Markup Language) and (Extensible style sheet language ).
Business logic: the business logic is applicable to session ejbs (which will be introduced later.
Data Access: entity ejbs and JDBC are suitable for data access.
Background system integration: the integration of the same background system may require many different technologies. The best requirements depend on the characteristics of the background system.
You may be surprised: why are there so many layers? In fact, a multi-tier approach can make enterprise-level applications highly scalable, allowing each layer to focus on specific roles. For example, the web server is responsible for providing pages and application servers to process application logic, while the database server provides database services.
Because J2EE is built on the Java 2 platform Standard Edition (j2se), it has all the advantages and functions of j2se. This includes the portability of "write once, available everywhere", the ability to access the database through JDBC, the ability to interact with the original enterprise resources, and a verified security model. Based on these, J2EE has added support for EJB (enterprise-level Java components), Java Servlets, Java Server Pages (JSPs), and XML technologies.
Distributed architecture and WebLogic application server
J2EE provides a framework-a set of standard APIs-for developing distributed architecture applications. The actual implementation of this framework is left to third-party vendors. Some vendors only focus on specific components in the entire J2EE architecture. For example, Apache Tomcat provides support for JSP and servlets, BEA System provides a complete implementation of the entire J2EE specification through its WebLogic application server product.
The WebLogic Server has simplified the process of establishing and deploying distributed applications with better scalability. Weblogic and J2EE handle a large number of common programming tasks on your behalf, including providing transaction services, security fields, reliable messages, name and directory services, database access and connection pools, thread pools, load balancing, and fault tolerance Processing.
By providing these public services in a standard and easy-to-use way, products such as WebLogic servers have created application systems with better scalability and maintainability, this increases availability for a large number of users.
J2EE Technology
In the following section, we will describe various technologies that constitute J2EE and learn how WebLogic servers support them in a distributed application. The most common J2EE technologies are JDBC, JNDI, EJB, JSP, and servlets. We will take a closer look at these technologies.
Java database connectivity (JDBC)
The jdbc api accesses a variety of databases in a unified manner. Like ODBC, JDBC hides different features of different databases for developers. In addition, because JDBC is built on Java, it also provides database access platform independence.
JDBC defines four different drivers, which are described as follows:
JDBC-ODBC Bridge Type 1
In the early stages of JDBC, The JDBC-ODBC bridge is obviously very useful, through the JDBC-ODBC bridge, developers can use JDBC to access ODBC data sources. However, you must install the ODBC driver on the client. In other words, you must install a certain version of Microsoft Windows. To use this type, you must sacrifice the independence of the JDBC platform. In addition, the ODBC driver must have the control permissions of the client.
Type 2: JDBC-native driver Bridge
The JDBC local driver bridge provides a JDBC interface, which is built on the top layer of the local database driver without using ODBC. The JDBC driver converts database APIs from standard JDBC calls to local calls. To use this type, you must sacrifice the platform independence of JDBC and install some local code on the client.
Type 3: JDBC-Network Bridge
The JDBC Network Bridge driver no longer needs the client database driver. It uses an intermediate server on the network to access the database. This application makes it possible to implement the following technologies, including Server Load balancer, connection buffer pool, and data cache. Because the 3rd types usually require less download time and platform independence, and do not require installation and control on the client, it is suitable for applications on the Internet.
Type 4: pure Java driver
The 4th types Use a pure Java Database driver to execute direct access to the database. This type actually implements a layer-2 Structure on the client. To be applied in the n-layer structure, a better way is to write an EJB that contains the access code and provides a service that is database independent to the client.
The WebLogic Server provides JDBC drivers for some common databases, including Oracle, Sybase, Microsoft SQL Server, and Informix. It also includes a JDBC driver for cloudscape, which is a pure Java DBMS and the WebLogic Server carries the evaluation version of the database.
Let's look at an instance.
JDBC instance
In this example, we assume that you have created a phonebook database in cloudscape and contain a table named contact_table, which has two fields: name and phone. First, load the cloudscape JDBC driver and request the driver manager to obtain a connection to the phonebook cloudscape database. Through this connection, we can construct a statement object and use it to execute a simple SQL query. Finally, we use a loop to traverse all the data in the result set and use the standard output to output the content of the name and phone fields.
Import java. SQL .*;
Public class jdbcexample
{
Public static void main (string ARGs [])
{
Try
{
Class. forname ("com. cloudscape. Core. jdbcdriver ");
Connection conn = drivermanager. getconnection ("JDBC: cloudscape: phonebook ");
Statement stmt = conn. createstatement ();
String SQL = "Select name, phone from contact_table order by name ";
Resultset = stmt.exe cutequery (SQL );
String name;
String phone;
While (resultset. Next ())
{
Name = resultset. getstring (1). Trim ();
Phone = resultset. getstring (2). Trim ();
System. Out. println (name + "," + phone );
}
}
Catch (exception E)
{
// Handle exception here
E. printstacktrace ();
}
}
}
OK. Next, let's take a look at how JDBC is used in enterprise applications.
Application of JDBC in enterprise applications
The above instances are actually very basic and may be insignificant. It assumes a 2-layer structure. In a multi-tier enterprise application, it is more likely that the client communicates with an EJB, And the EJB establishes a database connection. To achieve and improve scalability and system performance, the WebLogic Server provides support for connection buffer pool connection pools.
Connection Pool reduces the consumption of establishing and releasing database connections. After the system starts, you can create such a buffer pool. Later, if you have another request to the database, the WebLogic Server can easily retrieve data from the buffer pool. The data buffer pool can be defined in the weblogic. properties file of the WebLogic Server. (For more information, see the example in the weblogic. properties file)
Another common database feature of enterprise applications is transaction processing. Transactions are a set of declarative statements that must be processed as the same statement to ensure data integrity. By default, JDBC uses the auto-commit transaction mode. This can be achieved by using the setautocommit () method of the connection class.
Now we have some knowledge about JDBC, and we should switch to JNDI below.
Java Naming and Directory Interface (JNDI)
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, and the latter's objects on the application server.
In JNDI, each node in the directory structure is called context. Each JNDI name is relative to context. There is no concept of absolute name here. For an application, it can obtain its first context by using the initialcontext class:
Context CTX = new initialcontext ();
The app can use this initialized context to locate the resources or objects it needs through this directory tree. For example, suppose you expand an EJB on the WebLogic Server and bind the home interface to the MyApp name. myejb, a client of this EJB can locate the home interface through the following statement after obtaining an initialization context:
Myejbhome home = CTX. Lookup ("MyApp. myejb ");
In this example, once you have a reference to the requested object, the EJB home interface can call the method on it. We will introduce more in the "Enterprise Java Beans" section below.
The above discussion about JNDI is just a tip of the iceberg. To further search for objects in the context, JNDI also provides some methods to perform the following operations:
Insert or bind an object to the context. This is effective when you expand an EJB.
Removes objects from context.
Lists all objects in the context.
Creates or deletes sub-level context.
Next, we will start to focus on EJB.
Enterprise Java Beans (EJB)
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.
The EJB specification defines three basic bean types:
Stateless session beans: provides a single service that does not maintain any status. It cannot continue to exist when a Server failure occurs and has a short life cycle. For example, a stateless Session Bean may be used for temperature conversion calculation.
Stateful Session Bean: T provides session interaction with the client, and stores the status to represent a customer. A typical example is shopping cart. Stateful Session Bean cannot survive when the server fails, and its life is relatively short. Each instance is used only for a single thread.
Entity beans: provide consistent data representation-typically stored in the database-can continue to exist after a Server failure occurs. Multiple users can use EJB to represent the same data. A typical example of entity EJB is the customer's account information.
Despite the above differences, all ejbs have many similarities. They all process the home interface. It defines how a client creates and destroys ejbs. You can call the remote interface that defines the client method in bean, and the Bean class executes the main business logic.
The description of EJB development is beyond the scope of this article. However, if an EJB has been developed or purchased from a third party, it must be released on the application server. WebLogic Server 5.1 comes with an EJB deployer tool to help with EJB Publishing. When using the EJB deployer tool, you need to define the JNDI name used by the client to locate the EJB. Deployer tool generates Wrapper Classes to process and communicate with containers, and binds the requested Java classes together in a jar file.
Once the EJB is published, the client can use its JNDI name to locate the EJB. First, it must get a reference to the home interface. Then, the client can use this interface to call a create () method to obtain the handle of a bean instance running on the server. Finally, the client can use this handle to call the method in the bean.
After learning about EJB, let's look at JSP.
Assumerver pages (JSPs)
Many of us may already be familiar with Microsoft's Active Server Pages (ASP) technology. JSP corresponds to ASP, but it is more platform-specific. They are designed to help Web content developers create dynamic web pages and only require a relatively small amount of code. JSP can be used even if web designers do not know how to program, because JSP applications are very convenient. 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.
Let's take a look at a simple JSP instance. It only displays the current date and time of the server. Although the syntax is beyond the scope of this article, we can intuitively see that Java code is placed in the middle of the symbol, while Java expressions are placed between symbols.
Date jsp sample
The current date is.
You may have heard of JHTML sometimes. This is an old standard of JSP. The WebLogic Server supports both JSP and JHTML. Note that by default, JSP is not in a valid state on the WebLogic Server. To make it effective, you can edit the weblogic. properties file. If the Web server is not in a valid state, make it valid first. The situation of servlet is the same as that of JSP.
Below is: Java Servlets
Java Servlets
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.
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. A major difference between servlets and CGI scripts is that each CGI requires a new process to start at the beginning -- while servlets runs in a separate thread in the servlet engine. Therefore, servlets provides good scalability improvements.
When developing Servlets, you often need to extend the javax. servlet. http. httpservlet class and override some of its methods, including:
Service (): implements the command-definition method as a dispatcher.
Doget (): process http get requests from the client.
Dopost (): performs the http post operation.
Other methods include processing different types of HTTP requests. For more information, see the httpservlet API documentation.
The above describes various methods of the standard J2EE servlet API. The WebLogic Server provides a complete way to implement this API. Once you develop a servlet, you can register it in weblogic. properties and configure it on the WebLogic Server.
Through Java Servlets, we have reached the end of main J2EE technologies. But what J2EE provides does not end with this. The following section briefly describes some existing technologies, including RMI, Java IDL and CORBA, JTA, and XML.
Remote Method Invocation (RMI)
As its name indicates, the RMI protocol calls some methods on a remote object. It uses a continuous sequence to transmit data on the client and server. RMI is a lower-layer protocol used by EJB.
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 application with the legacy system.
Java transaction architecture (JTA)/Java Transaction Service (JTs)
JTA defines a standard API, which allows the application system to access various transaction monitoring data.
JTs is the basic implementation of the transaction monitoring function of CORBA ots. JTs specifies the implementation method of the Transaction Manager. The transaction manager is a Java image that supports the Java transaction API (JTA) specification at the high-level level and implements omg ots specification at a lower level. The JTs Transaction Manager is an application server, resource manager, and independent ol to help with the release of ejbs. When using the EJB deployer tool, you need to define the JNDI name used by the client to locate the EJB. Deployer tool generates Wrapper Classes to process and communicate with containers, and binds the requested Java classes together in a jar file.
Once the EJB is published, the client can use its JNDI name to locate the EJB. First, it must get a reference to the home interface. Then, the client can use this interface to call a create () method to obtain the handle of a bean instance running on the server. Finally, the client can use this handle to call the method in the bean.
After learning about EJB, let's look at JSP.
Assumerver pages (JSPs)
Many of us may already be familiar with Microsoft's Active Server Pages (ASP) technology. JSP corresponds to ASP, but it is more platform-specific. They are designed to help Web content developers create dynamic web pages and only require a relatively small amount of code. JSP can be used even if web designers do not know how to program, because JSP applications are very convenient. 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.
Let's take a look at a simple JSP instance. It only displays the current date and time of the server. Although the syntax is beyond the scope of this article, we can intuitively see that Java code is placed in the middle of the symbol, while Java expressions are placed between symbols.
Date jsp sample
The current date is.
You may have heard of JHTML sometimes. This is an old standard of JSP. The WebLogic Server supports both JSP and JHTML. Note that by default, JSP is not in a valid state on the WebLogic Server. To make it effective, you can edit the weblogic. properties file. If the Web server is not in a valid state, make it valid first. The situation of servlet is the same as that of JSP.
Below is: Java Servlets
Java Servlets
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.
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. A major difference between servlets and CGI scripts is that each CGI requires a new process to start at the beginning -- while servlets runs in a separate thread in the servlet engine. Therefore, servlets provides good scalability improvements.
When developing Servlets, you often need to extend the javax. servlet. http. httpservlet class and override some of its methods, including:
Service (): implements the command-definition method as a dispatcher.
Doget (): process http get requests from the client.
Dopost (): performs the http post operation.
Other methods include processing different types of HTTP requests. For more information, see the httpservlet API documentation.
The above describes various methods of the standard J2EE servlet API. The WebLogic Server provides a complete way to implement this API. Once you develop a servlet, you can register it in weblogic. properties and configure it on the WebLogic Server.
Through Java Servlets, we have reached the end of main J2EE technologies. But what J2EE provides does not end with this. The following section briefly describes some existing technologies, including RMI, Java IDL and CORBA, JTA, and XML.
Remote Method Invocation (RMI)
As its name indicates, the RMI protocol calls some methods on a remote object. It uses a continuous sequence to transmit data on the client and server. RMI is a lower-layer protocol used by EJB.
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 application with the legacy system.
Java transaction architecture (JTA)/Java Transaction Service (JTs)
JTA defines a standard API, which allows the application system to access various transaction monitoring data.
JTs is the basic implementation of the transaction monitoring function of CORBA ots. JTs specifies the implementation method of the Transaction Manager. The transaction manager is a Java image that supports the Java transaction API (JTA) specification at the high-level level and implements omg ots specification at a lower level. JTs Transaction Manager provides transaction services for application servers, resource managers, independent applications, and communication resource managers.
Javamail and JavaBeans activation framework
Javamail is an API used to access the mail server. It provides a set of abstract classes for the mail server. Not only supports SMTP servers, but also supports IMAP servers.
Javamail uses JavaBeans activation framework (JAF) to process MIME-encoded email attachments. Mime byte streams can be converted to Java objects or to Java objects. Therefore, most applications do not need to directly use JAF.
Java Messaging Service (JMS)
JMS 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 way to integrate your application with the legacy backend system.
Extensible Markup Language (XML)
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. At present, many different companies are working hard to combine Java and XML. For more information, visit Sun's Java-XML page or IBM developerworks's XML zone.
API ). 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 way to integrate your application with the legacy backend system.
Extensible Markup Language (XML)
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. At present, many different companies are working hard to combine Java and XML. For more information, visit Sun's Java-XML page or IBM developerworks's XML zone.