Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the underlying principle of Tomcat server?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the underlying principle of Tomcat server". In daily operation, I believe that many people have doubts about what is the underlying principle of Tomcat server. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the underlying principle of Tomcat server?" Next, please follow the editor to study!

What is Tomcat?

Tomcat is an open source Java Web application server that implements some of the technical specifications of Java EE, such as Java Servlet, Java Server Page, JSTL, and Java WebSocket. Java EE is a standard platform for enterprise-level applications launched by Sun, which defines a series of technical specifications for enterprise-level development. In addition to the above, there are EJB, Java Mail, JPA, JTA, JMS, and so on, all of which depend on the implementation of specific containers.

The figure above compares the implementation of the Java EE container. Both Tomcat and Jetty provide only the Servlet and JSP specifications that are necessary for the Java Web container. Developers need to rely on other open source implementations if they want to implement other features.

Glassfish is introduced by sun. After the latest specification of Java EE comes out, it will be implemented on Glassfish first, so it is the first choice to study the latest technology of Java EE.

The most common situation is to use Tomcat as the Java Web server, use the powerful out-of-the-box capabilities provided by Spring, and rely on other open source libraries to achieve responsible business functions.

Servlet container

Tomcat composition

As shown in the following figure, it is mainly composed of Container and Connector and related components.

Server: refers to the entire Tomcat server, which contains multiple sets of services, manages and starts each Service, and listens for shutdown commands sent from port 8005 to close the entire container.

Service:Tomcat encapsulates, provides complete, component-based web services, including two core components of Connectors and Container, as well as multiple functional components. Each Service is independent, but shares the resources of the same JVM.

The connector between Connector:Tomcat and the outside world listens to the fixed port to receive external requests, passes them to Container, and returns the results of Container processing to the outside world.

Container:Catalina,Servlet container, composed of multiple layers of containers, is used to manage the Servlet life cycle and call servlet-related methods

Loader: encapsulates Java ClassLoader for loading class files in Container; provides access authentication and role management mechanisms for web applications in Realm:Tomcat

The technical specification defined in JMX:Java SE is a framework for embedding management functions for applications, devices, systems, etc., and the running status of Tomcat can be monitored remotely through JMX.

Jasper:Tomcat 's Jsp parsing engine, which is used to convert Jsp into Java files and compile them into class files. Session: responsible for managing and creating session, as well as Session persistence (customizable), supporting session clusters.

Pipeline: acts as a pipeline in a container, where various valve (valves) can be set up, and requests and responses are handled through each valve in the pipeline, providing a flexible and configurable mechanism for handling requests and responses.

Naming: naming service, JNDI, Java naming and directory interface, is a set of API that accesses naming and directory services in Java applications. Naming services associate names with objects so that we can access objects by name. Directory service is also a naming service in which objects have not only names but also properties. JNDI can be used in Tomcat to define data sources and configuration information for separation of development and deployment.

Container composition

The top-level container of Engine:Servlet, which contains one or more Host child containers

Host: virtual host, responsible for the deployment of web applications and the creation of Context

Context:Web application context, including multiple Wrapper, responsible for parsing web configuration and managing all Web resources

Wrapper: the lowest-level container, which encapsulates Servlet and is responsible for the creation, execution and destruction of Servlet instances.

Life cycle management

In order to facilitate the management of the life cycle of components and containers, Tomcat defines a total of 12 states from creation, start, to stop and destroy.

Tomcat lifecycle manages the rule control of internal state changes. Components and containers only need to implement the corresponding lifecycle methods to complete the operations in each lifecycle (initInternal, startInternal, stopInternal, destroyInternal).

For example, when the initialization operation is performed, it will determine whether the current state is New, and if not, throw a lifecycle exception; if so, set the current state to Initializing and execute the initInternal method, which is implemented by the subclass. If the method executes successfully, the current state is set to Initialized, and if the execution fails, it is set to the Failed state.

The life cycle management of Tomcat introduces the event mechanism, which notifies the event listener when the life cycle state of the component or container changes, and the listener acts accordingly by judging the type of event. The addition of event listeners can be configured in the server.xml file

Tomcat the configuration process of all kinds of containers is carried out by adding listener, so that the configuration logic is decoupled from the container. Such as EngineConfig, HostConfig, ContextConfig.

EngineConfig: mainly prints start and stop logs

HostConfig: mainly deals with deploying applications, parsing application META-INF/context.xml and creating applications

Context ContextConfig: mainly parses and merges web.xml, and scans all kinds of web resources (filter, servlet, listener) of applications

The startup process of Tomcat

Starting from the start.sh script provided by Tomcat, the shell script calls the main method of Bootstrap, which actually calls the corresponding load and start methods of Catalina.

The load method parses the config/server.xml through Digester. In the process of parsing, the container is created based on the relationship and configuration information in the xml, and the relevant properties are set.

Catalina then calls the init and start methods of StandardServer to initialize and start the container.

According to the configuration relationship of xml, the child element of server is the child element of service,service is the top-level container Engine, and each layer container holds its own child container, and these elements implement various methods of lifecycle management, so it is easy to complete the lifecycle management of the entire container, such as startup and shutdown.

After StandardServer completes the init and start method calls, it keeps listening from port 8005 (configurable).

If a shutdown command is received, loop listening is exited, subsequent stop and destroy methods are executed, and the Tomcat container is closed.

The JVM's Runtime.getRuntime () room. addShutdownHook method is also called to close the container when the virtual machine exits unexpectedly.

All containers are inherited from ContainerBase, and the base class encapsulates the repeated work in the container, which is responsible for starting the container-related components Loader, Logger, Manager, Cluster, Pipeline, and the promoter container (thread pool concurrent promoter container, which submit multiple threads through the thread pool, returns the Future object after calling, and then calls the get method of the Future object to wait for the execution result).

List results = new ArrayList (); for (int I = 0; I < children.length; iTunes +) {results.add (startStopExecutor.submit (new StartChild (child [I]);} boolean fail = false; for (Futureresult: results) {try {result.get ();} catch (Exception e) {log.error (sm.getString ("containerBase.threadedStartFailed"), e); fail = true;}}

How to deploy Web applications

Note: catalina.home: installation directory; catalina.base: working directory; default value user.dir

Server.xml configures the Host element, specifies the appBase attribute, and defaults to\ $catalina.base/webapps/

Server.xml configure Context element, specify docBase, element, specify the path of web application

Custom configuration: configure the Context element in\ $catalina.base/EngineName/HostName/XXX.xml

HostConfig listens for events of the StandardHost container and parses the above configuration file in the start method:

Scan all folders and war packages under the appbase path, parse the META-INF/context.xml of each application, create a StandardContext, and add Context to the Host child container.

Parse all the Context configurations under $catalina.base/EngineName/HostName/, find the applicable location of the corresponding web, parse the META-INF/context.xml of each application, create a StandardContext, and add Context to the Host child container.

Note:

HostConfig does not actually parse Context.xml, but does it in ContextConfig.

Watched resource files (context.xml configuration files) are checked periodically in HostConfig

ContextConfig parses context.xml order:

Parse the global configuration config/context.xml first

Then parse Host's default configuration EngineName/HostName/context.xml.default

Finally, parse the applied META-INF/context.xml

ContextConfig parses web.xml order:

Parse the global configuration config/web.xml first

Then parse the default configuration of Host, EngineName/HostName/web.xml.default, and then parse the applied MEB-INF/web.xml

Scan the jar file under the application WEB-INF/lib/, parse the META-INF/web-fragment.xml, merge the xml and package it into WebXml, and set the Context

Note:

Scanning the web application and jar for annotations (Filter, Listener, Servlet) is done in the above steps.

Periodic execution of the container: backgroundProcess, implemented by ContainerBase, and threads are opened only in the top-level container. (backgroundProcessorDelay=10 flag bit to control)

Servlet lifecycle

Servlet is a server-side program written in Java. Its main function is to browse and modify data interactively and generate dynamic Web content.

The request arrives at the server, and the server maps to the corresponding Servlet according to the url

Determine whether the Servlet instance exists. If it does not exist, load and instantiate Servlet and call the init method

Server creates Request and Response objects respectively, and calls the service method of the Servlet instance (the corresponding doXXX method is called internally according to the http request method type within the service method)

The doXXX method is implemented by business logic. The request parameters are obtained from the Request object, and the result is returned to the caller through the response object after processing.

When Servlet is no longer needed by Server (usually when Server is turned off), Server calls the destroy () square method of Servlet.

Load on startup

When the value is 0 or greater than 0, the container loads the servlet; when the application starts. If it is a negative number or is not specified, it indicates that the container will not load until the servlet is selected. The lower the positive value, the higher the priority of starting the servlet.

Single thread model

Each time you visit servlet, create a new servlet entity object, but it does not guarantee thread safety, and tomcat limits the number of instances of servlet.

Request processing process

Listen for http or ajp requests based on the specified connector and port configured by server.xml

Establish a connection when the request arrives, parse the request parameters, create Request and Response objects, and call the invoke method of the top-level container pipeline

Layers of calls are made between containers, and finally the service method of the business servlet is called

Connector writes data from the response stream to socket

Pipeline and Valve

Pipeline can be understood as the real pipeline, Valve is the valve in the pipeline, Request and Response objects are processed and controlled by each valve in the pipeline.

There is an essential basic valve in the pipe of each container, and the others are optional. Basic valve is called last in the pipe and is responsible for calling the first valve of the child container.

The relationship between the three main methods in Valve: setNext, getNext, and invoke;valve is an one-way chained structure, and the invoke method itself calls the invoke method of the next valve.

The basic valve corresponding to each layer container is StandardEngineValve, StandardHostValve, StandardContextValve, and StandardWrapperValve.

JSP engine

JSP lifecycle

Compilation phase: servlet container compiles servlet source text

Components, generating the servlet class

Initialization phase: load the servlet class corresponding to JSP, create its instance, and call its initialization method

Execution phase: call the service method of the servlet instance corresponding to JSP

Destroy phase: call the destroy method of the servlet instance corresponding to JSP, and then destroy the servlet instance

JSP element

Code snippet:

JSP statement:

JSP expression:

JSP comments:

JSP directive:

JSP behavior:

HTML element: html/head/body/div/p/...

JSP implicit objects: request, response, out, session, application, config, pageContext, page, Exception

JSP element description

Code snippet: an Java statement, variable, method, or expression that contains any amount

JSP declaration: a declaration statement can declare one or more variables and methods for use by subsequent Java code

JSP expressions: output the value of Java expressions, in String form

JSP comments: comment code and comment out a piece of code

JSP directive: used to set properties related to the entire JSP page

Define the dependency properties of the page, such as language, contentType, errorPage, isErrorPage, import, isThreadSafe, session, etc.

Contains other JSP files, HTML files, or text files that are part of the JSP file and will be compiled and executed at the same time

Introduce the definition of tag library, which can be a custom tag

JSP behavior: jsp:include, jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:forward

JSP parsing process

Code snippet: output directly in the _ jspService () method

JSP declaration: output in the servlet class

JSP expression: output directly in the _ jspService () method

JSP comments: ignore directly, do not output

JSP instruction: distinguishes according to different instructions. Include: parses the introduced file; page-related attributes are used as attributes of JSP, which affects the behavior of parsing and request processing.

JSP behavior: different behaviors are handled differently. Jsp:useBean, for example, will obtain bean objects from pageContext according to the category of scope. If no bean is created, it will be saved in the pageContext of the corresponding scope.

HTML: output directly in the _ jspService () method

JSP implicit object: declared in the _ jspService () method and can only be used in the method

Connector

Http:HTTP is a hypertext transfer protocol, which is an application layer communication protocol between client browsers or other programs and Web servers.

The AJP:Apache JServ protocol (AJP) is a binary protocol that specializes in proxying inbound requests from the Web server to the application server located at the back end.

Blocking IO

Non-blocking IO

IO multiplexing

The difference between blocking and non-blocking lies in whether the system calls for read and write operations are blocked if the kernel state has no data to read or buffer space to write.

The advantage of IO multiplexing is that it can listen to the readable and writable events of multiple socket at the same time, so that the application can listen to multiple socket at the same time, freeing up application thread resources.

Comparison of all kinds of Connector in Tomcat

There are three implementation modes of Connector, which are BIO, NIO, and APR, which can be specified in server.xml.

JIO: a TCP module written in java.io that blocks IO

NIO: TCP module written in java.nio, non-blocking IO, (IO multiplexing)

APR: full name Apache Portable Runtime, which uses JNI to read files and transfer them over the network.

Apache Portable Runtime is a highly portable library that is the core of Apache HTTP Server 2.x.

APR has many uses, including access to advanced IO functions (such as sendfile,epoll and OpenSSL), operating system-level functions (random number generation, system state, etc.), and local process processing (shared memory, NT pipes, and Unix sockets).

Description of the meaning of the fields in the table:

Support Polling: whether socket event polling based on IO multiplexing is supported

Polling Size: maximum number of connections polled

Wait for next Request: whether the processing thread is released while waiting for the next request, BIO is not released, so the number of concurrent connections processed in the case of keep-alive=true is limited.

Read Request Headers: due to the lack of request header data, it can be parsed by the container ahead of time without blocking

Read Request Body: reading request body data is a matter of applying business logic. At the same time, the limitation of Servlet requires blocking reading.

Write Response: similar to the logic of reading request body, blocking writes are also required.

NIO handles related classes

The Acceptor thread is responsible for receiving the connection, calling the accept method to block the connection, encapsulating the socket into PollerEvent, specifying the registered event as op_read, and putting it into the EventQueue queue. The run method of PollerEvent registers the Selector with the specified event of socket.

The Poller thread gets the PollerEvent from EventQueue, executes the run method of PollerEvent, calls the select method of Selector, creates a Http11NioProcessor if there is a readable Socket, and puts it into the thread pool for execution.

CoyoteAdapter is the adapter from Connector to Container. Http11NioProcessor calls the service method it provides, internally creates Request and Response objects, and calls the invoke method of the first Valve in the top-level container's Pipeline.

Mapper mainly deals with the parsing of http url-to-servlet mapping rules and provides map methods.

Main parameters of NIO Connector

Comet

Comet is a push technology for web that enables the server to transmit updated information to the client in real time without requiring the client to make a request

Before WebSocket comes out, if comet is not applicable, server-side push can only be simulated by polling Server on the browser side.

Comet supports servlet asynchronous processing IO, triggering events when data is readable on the connection, and writing data asynchronously (blocking).

To implement Comet, Tomcat only needs to inherit HttpServlet and implement CometProcessor interface.

Begin: a new request connection access call, which can initialize objects related to Request and Response, and save response objects for subsequent data writing

Read: called when the request connection has data to read

End: when the data is available, it is called if it is read to the end of the file or when response is closed

Error: called when an exception occurs on the connection, data reading exception, connection disconnection, handling exception, socket timeout

Note:

Read: if there is data in the post request but is not processed in the begin event, read will be called. If the read does not read the data, the Error callback will be triggered and socket will be closed.

End: also called when socket times out and response is closed; called when server is closed

Error: except that socket timeout will not close socket, all others will close socket.

The current comet session should be closed when End and Error time is triggered, that is, the close method of CometEvent should be called

Note: do thread-safe operations when events are triggered

Asynchronous Servlet

Traditional process:

First, after Servlet receives the request, the request data is parsed

Then, some methods of the business interface are called to complete the business processing.

Finally, the response is submitted based on the result of the processing, and the Servlet thread ends.

Asynchronous processing process:

The client sends a request

The Servlet container allocates a thread to process a servlet in the container

Servlet calls request.startAsync (), saves the AsyncContext, and returns

Container threads that exist in any way will exit, but the response remains open

The business thread uses the saved AsyncContext to complete the response (thread pool)

The client receives a response

The Servlet thread transfers the request to an asynchronous thread to perform business processing, and the thread itself returns to the container. Servlet has not yet generated the response data. After the asynchronous thread has finished processing the business, it can generate the response data directly (the asynchronous thread has references to ServletRequest and ServletResponse objects).

Why is asynchrony supported in web applications?

Asynchrony is introduced mainly for requests that are time-consuming: such as a slow database query, an external REST API call, or other I-sign O-intensive operations. Such time-consuming requests quickly deplete the thread pool of the Servlet container, which in turn affects scalability.

Note: from the client's point of view, request is still like any other HTTP request-response interaction, but it takes longer.

Asynchronous event snooping

Triggered when onStartAsync:Request calls the startAsync method

Triggered when onComplete:syncContext calls the complete method

OnError: triggered when an exception occurs in the process of processing the request

OnTimeout:socket timeout trigger

Note:

After onError/ onTimeout is triggered, onComplete will be called back immediately.

After onComplete is executed, request and response can no longer be operated.

At this point, the study of "what is the underlying principle of Tomcat server" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report