The phrase “Tomcat is a Servlet container” should be familiar to programmers in 2019.

Simply thinking about this statement, we can abstract this code:

class Tomcat {
	List<Servlet> sers;
}
Copy the code

If Tomcat looks like this, it won’t work. So, Tomcat looks like this:

class Tomcat {
    Connector connector; // Connect to the processor
	List<Servlet> sers;
}
Copy the code

Regardless of the Connector’s underlying implementation, let’s just know that the Connector handles the request.

So let’s think about the container.

In addition, I have compiled the interview questions for 20 years, including Spring, concurrency, database, Redis, distributed, Dubbo, JVM, microservices and other aspects of the summary, if you need to get:Tencent document

Context

As the name suggests, Servlet containers are used to load and store servlets.

A Servlet represents a program running on the server side (Servlet = Server + applet). In order to use this program, users need to send requests to the program and get the response from the program, namely ServletRequest and ServletResponse in the Servlet specification.

So servlets are the Java specification for handling requests, and our projects often have one or more servlets that receive requests or pass them on to other business logic.

So both our Spring MVC and Spring Boot have a DispatcherServlet (a Servlet with similar functionality that receives requests).

So, usually servlets belong to an application (project), in other words, our application contains multiple servlets, so this is the second layer of Servlet container — the application, or Tomcat Context. What about the first layer Servlet container?

Wrapper

The Wrapper is the first layer Servlet container. The Wrapper represents the Wrapper for the Servlet, so it is the closest thing to a Servlet. So why do you need a Wrapper? We usually think of wrappers as something like this:

class Wrapper {
	Servlet servlet;
}
Copy the code

There is one Wrapper for each Servlet, so there is no need for a Wrapper, but there are a few other things to consider:

  • For example, a Filter can correspond to a Servlet.
  • For example, ServletPool, where servlets are common to all requesting threads, allows each requesting thread to use a separate Servlet instance.

So in the Wrapper, you don’t just have a Servlet, you also have filters and Servlet pools, so the Wrapper is the first Servlet container.

Host

In our real life, an application is deployed on a Host, so a Host can contain multiple applications, and an application can contain multiple servlets, so Host is the third layer container.

In Tomcat, Host represents a virtual Host. When Tomcat processes a request, it can enter the corresponding Host for processing according to the requested domain name.

Engine

Host manages the Context, Context manages the Wrapper, Wrapper manages the Servlet, and Engine manages hosts. So Engine is the fourth layer container.

The last

I’m sure some of you are wondering if you don’t need containers on Engine? No? Here’s an example:

Our money (Servlet) goes in the Wrapper, the wallet goes in the Context, the bag goes in the Host, and the suitcase goes in the Engine.

So, if you ask me, “Where’s Engine?” It’s like asking me, “Where’s the plane?”

The answer is that you don’t need higher-level containers anymore, because you don’t need them.

conclusion

In Tomcat, containers are divided into:

  1. Wrapper
  2. Context
  3. Host
  4. Engine

The final routine: a thumbs-up, good luck, attention, youth forever…