A brief introduction to Tomcat's overall structure

A brief introduction to Tomcat's overall structure

Tomcat is widely known as a web container. It has accompanied my entire programming career, from the time I started learning Java to my current work. Tomcat is essentially a Servlet container. What a Servlet can do is: process request resources and fill in response objects for the client.

Tomcat is responsible for loading the Servlet class we wrote, calling the Servlet's init() method, creating a servletRequest and a servletResponse instance for a request, calling the servlet's service() method, passing the servletRequest and servletResponse as parameters, and calling destroy() to uninstall the servlet when closing it. Next, I will briefly introduce the overall structure of Tomcat.

Tomcat's overall structure

As shown in the figure, the two main components of Tomcat are the connector and the container. Multiple connectors and a container form a service. The service is used to provide services to the outside world, and the life cycle of the service is controlled by the server. Server belongs to the top-level abstraction.

The connector is used to handle things related to network connections, such as socket connections, request encapsulation, connection thread pools, etc. The container mainly handles the requests accepted by the connector. Service is just an extra layer around Connector and Container, assembling them together to provide services to the outside. A Service can have multiple Connectors but can only have one Container. The lifecycle of all components is managed uniformly using the lifecycle interface, which includes init, start, stop, and destroy methods.

The original connector could only be set to BIO mode. Now the default connection mode of high-level Tomcat versions is NIO, which greatly improves the concurrency of requests.

There are four types of containers in Tomcat, from top to bottom: engine, host, context, and wrapper. A wrapper corresponds to a servlet, a context corresponds to an application, a host corresponds to a site, and an engine is an engine. There is only one container. The startup between containers is done using

Tomcat container model

This is a brief introduction to the entire structure of Tomcat. Next, we will deepen our understanding through the processing flow of a Tomcat request. Assuming the request is: http://localhost:8080/test/index.jsp, the processing flow of Tomcat is as follows

1. The request is sent to port 8080 and is received by the connector.

2. The connector passes the request to the engine of its service for processing and waits for the engine to respond.

3.engine obtains the request address and matches the virtual host host

4. The engine matches the host named localhost, which receives the request /test/index.jsp, matching the context owned by the host

5. The host matches the context with path /test. If no match is found, it is handled by the empty context.

6. The context gets the request/index.jsp and looks for the corresponding servlet in the mapping file

7. The context matches the servlet with the pattern *.jsp and finds the corresponding JspServlet class (Jsp will eventually be converted into Servlet)

8. Construct httpservletrequest and httpServletResponse objects and use them as parameters to call doGet or doPost of JspServlet

9.context returns the response to the host

10. The host returns the response to the engine

11. The engine returns the response to the connector

12. The connector returns the response to the user's browser, and the request ends

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Python uses pipeline to batch read and write redis
  • Python: Detailed explanation of the use of Item Pipeline components in the Scrapy framework
  • Detailed explanation of Java using Pipeline to read and write Redis batches (hmset & hgetall)
  • Detailed explanation of the significant performance improvement of redis using pipeline (PipeLine) and batch (Batch) operations
  • Scrapy custom pipeline class implements the method of saving collected data to MongoDB
  • A solution to the abnormal exit of Tomcat caused by semaphore
  • Detailed explanation of pipeline and valve in tomcat pipeline mode

<<:  Java imports data from excel into mysql

>>:  An Incomplete Guide to JavaScript Toolchain

Recommend

Solve the problem of using swiper plug-in in vue

Since I used this plugin when writing a demo and ...

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...

Installation and configuration of MySQL 5.7.17 free installation version

MYSQL version: MySQL Community Server 5.7.17, ins...

Summary of common functions of PostgreSQL regular expressions

Summary of common functions of PostgreSQL regular...

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...

Basic ideas and codes for implementing video players in browsers

Table of contents Preface Summary of audio and vi...

Incomplete solution for using input type=text value=str

I encountered a very strange problem today. Look a...

JavaScript implements Tab bar switching effects

Here is a case that front-end developers must kno...

3 different ways to clear the option options in the select tag

Method 1 Copy code The code is as follows: documen...

uni-app implements NFC reading function

This article shares the specific code of uni-app ...

Does the website's text still need to be designed?

Many people may ask, does the text on the website...

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...