Tomcat is a Servlet container?

"Tomcat is a Servlet container", this sentence in 2019 for the programmers should be familiar.

Consider this simple sentence, so we can abstract out a piece of code:

class Tomcat {
    List<Servlet> sers;
}
复制代码

If Tomcat on a long way, then it certainly will not work, so, in fact, Tomcat is this:

class Tomcat {
    Connector connector; // 连接处理器
    List<Servlet> sers;
}
复制代码

We are not here to consider the underlying Connector implementation, we only need to know Connector is responsible for processing the request.

We still think about the container .

Context

As the name suggests, Servlet container is used to store the Servlet load.

A Servlet represents a program executed on the server (servlet = server + applet). User wants to use such a program, the program needs to send requests to and acquired in response to the program, i.e. Servlet specification ServletRequest, ServletResponse.

So in fact, Java Servlet to deal with a specification in the request, so our project will usually have one or more of Servlet, which it has to be responsible for receiving the request, or the request to the other business logic.

So our Spring MVC, Spring Boot there is a DispatcherServlet (a Servlet similar functions, is responsible for receiving the request).

Therefore, usually belonging to a Servlet application (program), in other words, our application comprises a plurality of Servlet, so this is the second layer Servlet Container - application, i.e. in Tomcat Context (application context) . Then the first layer Servlet container it?

Wrapper

Wrapper is the first layer Servlet containers, Servlet Wrapper represent packagers, so it is the closest Servlet, then why Wrapper it?

We usually think Wrapper is this:

class Wrapper {
    Servlet servlet;
}
复制代码

A Wrapper corresponds to a Servlet, so to think, then, you do not need Wrapper, but we have to consider some other cases:

  • For example Filter, Filter is a corresponding one of the Servlet.
  • For example ServletPool, usually the Servlet requesting thread is common to all, but the support request every thread in a Servlet using separate Servlet examples alone.

Therefore, in the Wrapper, including not only a Servlet, further comprising a filter tank and Servlet, so Wrapper is the first layer Servlet Container .

Host

In our real life, an application is deployed on a host computer, so a host can contain multiple applications, an application contains multiple Servlet, therefore, Host is the third layer of container. In the Tomcat, Host represents a virtual host, Tomcat in processing the request, the Host can enter the appropriate domain name according to the request is processed.

Engine

Host Management Context, Context Management Wrapper, Wrapper management Servlet, while the Engine is used to manage the Host. Engine is therefore the fourth layer container.

At last

Sure some people have doubts, then the top of the container Engine does not require it? no need? for example:

Our money (Servlet) to your wallet (Wrapper), in your wallet to be placed in the bag (Context), the bags to be placed in the trunk (Host), the trunk should be placed Aircraft (Engine) on.

So, if you ask me "Engine put what?" Is equivalent to ask me, "which put the aircraft?"

The answer is no longer required a higher level of container, because not necessary .

to sum up

In Tomcat, the container is divided into:

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

Article is very short, not too long time to write articles, take the time today to express my brief Tomcat understanding of the concept of "container", I hope we can still be harvested it. We can focus on my public No. 1:25 , continuously updated on new technology and new thinking to understand.

Guess you like

Origin juejin.im/post/5dc933ec518825481e4372d9