Introduction to Tomcat theory

Source: Internet
Author: User


Let's review the servlet and JSP before we introduce Tomcat.


Servlet is a CGI technology that enables Java to develop dynamic Web resources and communicate with the front-end Web server through CGI, but its definition of HTML documents has to be implemented by Java programs, and any static resources to be modified will have to recompile the entire Java page program. , very troublesome, and Java programmers must understand how the front-end HTML is presented

Servlets can also be thought of as server-side applets. The servlet is loaded and executed by the Web server as if the applet were loaded and executed by the browser. The servlet receives the request from the client (through the Web server), executes some kind of job, and returns the result. The basic process for using Servlets is as follows:


1. Client requests are made via HTTP.

The 2.WEB server receives the request and sends it to the servlet. If the servlet has not yet been loaded, the Web server will load it into the Java virtual machine and execute it.

3.servlet will receive the HTTP request and perform some sort of processing.

4.servlet will return an answer to the Web server.

The 5.WEB server sends an answer received from the servlet to the client.


Because Servlets are executed on the server, there is usually no need to implement the security issues associated with applets. Note that the Web browser does not communicate directly with the servlet, the servlet is typically loaded and executed by the front-end Web server, and the servlet is written in Java, so the servlet is written once and can be run on any platform (write Once,run Anywhere).



JSP is a scripting language that allows Java programs to be embedded in HTML documents in the form of a label <%%>. This greatly facilitates the compilation and modification of the program (the Servlet program is modified (that is, even if the HTML static resource modification) to recompile the entire Java Program page), JSP modified can immediately see the results, do not need to compile manually, the JSP engine will do the work , but Servelt needs to compile, restart the servlet engine and a series of actions


However, the JSP program (usually the end of the. JSP) must first be translated (such as Jasper) as the. Java source code,. Java is then compiled into a. Class class by the compiler and then loaded and interpreted in the JVM for execution


In short, compared to the servlet, JSP can easily combine Java and HTML, so that programmers can easily implement in the HTML static document to add or modify the dynamic content of Java writing, which is also the only difference between the servlet and JSP, the other difference is not very





Tomcat server is a free open source Web application server, belongs to the lightweight application server, in small and medium-sized systems and concurrent access users are not widely used, is the first choice to develop and debug JSP programs; Generally, Web sites developed in JSP format, Almost all use Tomcat as the Java application Server. Tomcat is not strictly speaking of a web Container (with jvm,jasper functionality, Can be processed directly to the. JSP program, you can let the JSP write Dynamic Web Resources in Tomcat run, and will run the results directly to the client or to the front-end Web server to respond to the client


Tomcat's cluster is typically the front-end of Apache as a proxy server, the user's. JSP requests are forwarded to Tomcat at the backend for processing, and when configured correctly, Apache serves the HTML page, and Tomcat actually runs the JSP page and servlet. In addition, like a Web server such as IIS, Tomcat has the ability to handle HTML pages, and it is also a servlet and JSP container, and the standalone servlet container is the default mode for Tomcat. However, Tomcat is less capable of handling static HTML than the Apache server. Currently, the latest version of Tomcat is 9.0.



The component composition of Tomcat

650) this.width=650; "src=" Http://img1.51cto.com/attachment/201309/18/2033581_13794842315fTS.png "alt=" Tomcat News "/>



Tomcat's architecture:

Tomcat 6 supports the specification of Servlet 2.5 and JSP 2.1, which consists of a set of nested hierarchies and components and can generally be divided into the following four categories:

1. Top-level components: At the top level of the configuration hierarchy, and have a strict correspondence between each other; Server, Service

2. Connector: Connect client (can be browser or Web server) request to servlet container,

3. Container: Contains a set of other components; Engine, Host, Context

4. Nested components: In one container, but cannot contain other components;


Common components:

1. Server: An instance of Tomcat, typically a JVM can contain only one Tomcat instance, so a single instance of Tomcat can be started on a physical server in each JVM with multiple JVMs, each of which belongs to a separate management port. This is a top-level component.


2. Service: A service component typically contains one engine and one or more connectors associated with the engine. Naming the service makes it easy for administrators to identify logs generated by different services in the log file. A server can contain multiple service components, but typically one server is assigned to only one service.


Connector Class components:

3, connector (connectors): Responsible for connecting the client (can be a browser or Web server) request to the Servlet container within the Web application, usually refers to receive the client sent to request the location and server-side allocation of the port. The default port is typically 8080 of the HTTP protocol, and administrators can change the port to suit their needs. One engine can configure multiple connectors, but these connectors must use different ports. The default connector is http/1.1-based coyote. At the same time, Tomcat supports AJP, Jserv, and JK2 connectors.


Container Class Components:

4. Engine: The engine usually refers to the servlet engine component that processes the request, the Catalina Servlet engine, which examines each request's HTTP header information to identify which host or context the request should be sent to. and returns the requested processing result to the appropriate client. In a strict sense, a container does not have to be implemented by an engine, it can be just a container. If Tomcat is configured as a standalone server, the default engine is an already defined engine. If Tomcat is configured as the back end of the Apache Web server that provides the servlet functionality, the default engine is ignored because the Web server itself can determine where to send the user request. An engine can contain multiple host components.

5. Host: The host component is similar to the virtual host in Apache, but only the FQDN-based "virtual host" is supported in Tomcat. An engine must contain at least one host component, but when the engine has multiple hosts, the request arrives at the engine to decide which host to send to or the user requests a non-existent one, so it is generally necessary to specify a default virtual host for the engine, and each virtual host can be a separate Web site.

6. Contextual: The context component is the most hierarchical component that represents the Web application itself. The most important thing to configure a context is to specify the root directory of the Web application so that the servlet container can send user requests to the correct location. The context component can also contain custom error pages to provide friendly hints when a user accesses an error, and each context can deploy an application separately.


Container class components: Engine, Host, Context

Top-level components: Server, Service


A service can only have one engine, but one or more connector (connectors) can be linked to the engine, an engine can have more than one host, a host can have multiple context


There are also other nested class (nested) components:

Such components are typically contained in container class components to provide services with management capabilities, they cannot contain other components, but some can be configured by different tiers of containers individually.

7. Valve: Used to intercept a request and perform some sort of processing before transferring it to the target, similar to the filter defined in the servlet specification. Valve can be defined in the components of any container class. Valve is often used to record information such as client requests, client IP addresses, and servers, and this processing technique is often referred to as request dumps (requests dumping). The request dump Valve records the HTTP header information and cookie information file in the request client request packet, and the response dump Valve records the header and cookie information of the response packet into the file.

8, Logger (Logger): Used to record the status information inside the component, can be used in any container other than the context. Logging functionality can be inherited, so an engine-level logger will record information about all components within the engine, unless an internal component defines its own logger component.

9, Realm (Realm): For the user's authentication and authorization; When you configure an application, the administrator can define roles and permissions for each resource or resource group, and these access control functions need to be implemented through Realm. Realm authentication can be implemented based on text files, database tables, LDAP services, and so on. Realm's utility extends across the entire engine or top-level container, so all applications within a container share user resources. At the same time, realm can be inherited by the subcomponents of its component, or it can be overridden by realms defined in the quilt component.



Tomcat Connector Architecture:

Based on Apache as a tomcat front-end architecture, Apache uses the MOD_JK, MOD_JK2, or Mod_proxy modules to exchange data with the back-end tomcat. For Tomcat, each Web container instance has a connector module component developed in the Java language, and in Tomcat6, the connector is the Org.apache.catalina.Connector class. Constructors of this class can construct two categories of connectors: http/1.1 is responsible for responding to requests based on the HTTP/HTTPS protocol, and ajp/1.3 is responsible for responding to AJP-based requests. However, you can simply implement the creation of a connector in the Server.xml configuration file, but the class used to create it differs depending on whether the system supports APR (Apache portable Runtime).

Apr is a collection of local libraries attached to a communication layer on the operating system that provides a common and standard API, which is capable of balancing utility when using APR-capable applications to provide better scalability when communicating with Apache.

At the same time, it should be explained that the MOD_JK2 module is no longer supported, the MOD_JK module is currently supported by Apache, but its project activity has been greatly reduced. Therefore, the more common way is to use the Mod_proxy module at present.


If APR is supported:

1, Http/1.1:org.apache.cotote.http11.http11aprprotocol

2, Ajp/1.3:org.apache.coyote.ajp.ajpaprprotocol

If APR is not supported:

Http/1.1:org.apache.coyote.http11.http11protocol

Ajp/1.3:org.apache.jk.server.jkcoyotehandler


Connector protocol:


The Tomcat Web server Connector supports two protocols: AJP and HTTP, which both define the data transfer between the Web server and Tomcat in binary format and provide the appropriate control commands.


AJP (Apache jserv Protocol) protocol:

If the tomcat cluster front-end is Apache based on its own PROXY_MOD module as a reverse proxy, Apache can send the HTTP request of the front end to the back-end Tomcat with the AJP protocol, The AJP protocol is a binary-based format for transferring data between a Web server and tomcat


HTTP protocol: If the front end of the tomcat cluster is a reverse proxy implemented by Nginx, it uses the HTTP or HTTPS protocol to establish communication between the Web server and Tomcat, when Tomcat is a fully functional HTTP server. It needs to listen on a port to receive requests from a pre-business Server.


Its use of HTTP protocol forwarding requests is not as efficient as AJP's binary format, so it is generally recommended that the Tomcat cluster is Apache, and Apache and Tomcat are open source software produced by ASF (Apache software Foundation).



Tomcat working mode

(1). Stand-alone servlet container

The default working mode of Tomcat, as a standalone servlet container, is a part of the built-in Web server and refers to the case of using a Java-based Web server. The other two approaches are the way Tomcat integrates with other servers.

(2). In-Process servlet container

servlet containers are implemented as plug-ins and Java containers for Web servers. The Web server's plug-in opens a JVM in the internal address space (Java VIRTUAL Machine) that enables the Java container to run internally. If there is a request to invoke the servlet, the plug-in will take control of the request and pass it (using JNI) to the Java container. In-process containers are ideal for multi-threaded, single-process servers, and provide a good run speed, but with insufficient scalability.

Note that JNI is the abbreviation for Java NATIVE interface, which is a Java native calling interface that communicates with local programs written in other languages through the Jni,java program.

(3). Out-of-process servlet container

The servlet container runs in an address space outside the Web server and uses IPC (such as TCP/IP) to communicate as a Web server's plug-in and JVM. Out-of-process containers have less reaction time than in-process containers, but have better scalability, stability and other performance.

The IPC interprocess is shorthand for communication (interprocess communication), which is a technique for implementing interprocess communication.


6.Tomcat the relationship between the components

Tomcat is a component-based server, and its constituent components are configurable, the outermost of which is the Catalina servlet container, and the other components are configured in this top-level container according to the format requirements. The various components of Tomcat are configured in the Server.xml file, and the Tomcat server has default implementations for various components by default, and Server.xml is the core configuration file for Tomcat, which describes the relationships between the various components of Tomcat


<Server> Top-level components, representing a server

<Service> Top-level components, a collection of connector, a Service can only have one engine

<Connectior/> connectors, multiple connectors can be associated to the same engine

<Engine> container class components, which handle all customer requests for a specific service component, can contain multiple host

<Host> container class components to handle all customer requests for a specific virtual host

<Context> container class components to handle all customer requests for a specific Web application

</Context>

</Host>

</Engine>

</Service>

</Server>


The three components in Tomcat that really handle customer requests and generate responses are engine, Host, Context.


such as Tomcat's handling of user requests and diagrams of components:




650) this.width=650; "src=" Http://img1.51cto.com/attachment/201309/18/2033581_1379484232QtTw.png "alt=" tomcat organization "/ >







































This article is from the "11097124" blog, please be sure to keep this source http://11107124.blog.51cto.com/11097124/1888132

Introduction to Tomcat theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.