Is Tomcat a Servlet container?

Consider this sentence. We can abstract this code:

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

If this is what Tomcat looks like, it won’t work, so this is what Tomcat looks like:

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

Regardless of the underlying implementation of the Connector, let’s just know that the Connector is responsible for handling requests.

Let’s think about the container.

Context is just what the name suggests

The Servlet container is used to load and store servlets.

A Servlet represents a program running on a server (Servlet = server + applet). To use such a program, a user needs to send a request to the program and obtain a response from the program, which is called a ServletRequest or ServletResponse in the Servlet specification.

So servlets are essentially a specification for handling requests in Java, so our projects often have one or more servlets that take care of receiving requests or forwarding them to other business logic.

So both our Spring MVC and Spring Boot have a DispatcherServlet (a similar Servlet 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, which is the Context in Tomcat. What about the layer 1 Servlet container?

Wrapper

Wrapper is the first layer Servlet container, Wrapper represents the Wrapper of the Servlet, so it is the closest to the Servlet, so why do you need a Wrapper?

We usually think of a Wrapper like this:

class Wrapper {
	Servlet servlet;
}
Copy the code

A Wrapper for a Servlet. If you think of it this way, you don’t really need a Wrapper, but there are other scenarios to consider:

  • For example, a Filter can correspond to a Servlet.

  • For example, ServletPool, a common Servlet is common to all request threads, but each request thread in the Servlet is supported to use a separate Servlet instance.

So in a Wrapper, there is not only a Servlet, but also filters and pools of servlets, so the Wrapper is the first layer of the 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 a Host is the third layer container.

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

Engine

Host manages Context, Context manages wrappers, wrappers manage servlets, and Engine manages hosts. So Engine is a tier 4 container.

The last

I’m sure some people are wondering, is there no need for containers on Engine? No need? 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 do I put the Engine?” It’s like asking me, “Where’s the plane?”

The answer is that higher level containers are no longer needed because they are not necessary.

conclusion

In Tomcat, containers are divided into:

  1. Wrapper

  2. Context

  3. Host

  4. Engine