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

JavaScript to achieve skin effect (change background)

This article shares the specific code of JavaScri...

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

Linux CentOS6.9 installation graphic tutorial under VMware

As a technical novice, I am recording the process...

Beginners learn some HTML tags (2)

Related article: Beginners learn some HTML tags (1...

How to modify the scroll bar style in Vue

Table of contents First of all, you need to know ...

Use personalized search engines to find the personalized information you need

Many people now live on the Internet, and searchin...

Briefly understand the MYSQL database optimization stage

introduction Have you ever encountered a situatio...

Detailed explanation of JavaScript to monitor route changes

Table of contents history pushState() Method push...

Detailed steps for yum configuration of nginx reverse proxy

Part.0 Background The company's intranet serv...

Introduction to Computed Properties in Vue

Table of contents 1. What is a calculated propert...

Front-end JavaScript Promise

Table of contents 1. What is Promise 2. Basic usa...

Delete the image operation of none in docker images

Since I usually use the docker build command to g...

Vue basics MVVM, template syntax and data binding

Table of contents 1. Vue Overview Vue official we...