How Tomcat works (v)-tomcat container

Source: Internet
Author: User
Tags wrapper tomcat

A Tomcat container needs to implement Org.apache.catalina.Container, and you can use the Setcontainer method in the Tomcat connector to pass a container to the connector. Container Interface

The first thing to note is that in concept Tomcat has four types of containers: The engine represents the entire Catalina servlet engine. Host represents a Web application with a virtual host with several contexts, a context that has several Wrapper Wrapper representing a single servlet

Each of these containers has a corresponding interface in the Org.apache.catalina package. These interfaces are inherited from container, while Standardengine, Standardhost, Standardcontext, and Standardwrapper are their standard implementation classes
The following diagram is their UML diagram:

An "upper-level" container can have 0 or more "downlevel containers" as a child, such as a context can have multiple wrapper, but wrapper as the bottommost container, cannot have child. The Container interface supports the following:
public void AddChild (Container);
public void RemoveChild (Container);
Public Container findchild (String name);
Public container[] Findchildren ();
A container can also have other components, such as Loader,logger,manager, Realm, and resources, which will be discussed in a later section.
What's more interesting is that the Container interface is designed to be a pattern that TOMCAT administrators can use Server.xml file configuration to determine how it works by using a pipeline (pipelining) and a series of valves that will be in the next section discussed in Pipelining Task. pipelining Tasks (pipeline Task)

The main topics to be discussed here are the four interface pipeline (task flow), Valve, Valvecontext, and Contained.
A task flow contains all the tasks that the container will perform. A valve represents a specific task, there is a basic valve in the task flow, and you can optionally add valves as needed, the number of valves is defined as the number of valves added (excluding basic valves). The valve can be added by editing the Server.xml.
If you already understand the servlet filter, then the task flow and the way its valves work is not hard to imagine. A task flow is like a filter chain and a valve is like a filter, as with a filter, a valve can manipulate the request and response methods passed to it. Once a valve has been processed, the next valve in the pipeline is further processed, and the basic valve is always called at the end.
A container can have a pipeline. When the container's Invoke method is called, the container will process the valve in the pipeline and process it one after the other until all the valves have been processed. The pseudo-code of the pipelining invoke method can be imagined as follows:

Perform the added valve for
(int n=0; n<valves.length; n++) {
    valve[n].invoke (...);
}
Perform basic valve
basicvalve.invoke (...);

However, Tomcat's designers chose a different way of dealing with the org.apache.catalina.valves.ValveBase definition, Under the Org.apache.catalina.valves package, a variety of different valves are defined. They inherit from the Valvebase and implement the Invoke method in valve. After creating a valve object, the valves call GetNext to continue with the next method.
Now look at the details of each interface: the Pipeline Interface task Flow interface

Public  interface Pipeline
{
  public  Valve getbasic ();
  Public  void Setbasic (Valve paramvalve);
  Public  void Addvalve (Valve paramvalve);
  Public  valve[] getvalves ();
  Public  void Removevalve (Valve paramvalve);
  Public  Valve GetFirst ();
  Public  Boolean isasyncsupported ();
  Public  Container GetContainer ();
  Public  void Setcontainer (Container paramcontainer);
The valve Interface Valves Interface
Public  interface Valve
{
  public  valve GetNext ();
  Public  void Setnext (Valve paramvalve);
  Public  void Backgroundprocess ();
  Public  void Invoke (Request paramrequest, Response paramresponse)
  throws IOException, servletexception;
  Public  Boolean isasyncsupported ();
}
The Contained Interface

A valve can selectively implement the Org.apache.catalina.Contained interface. The interface defines that its implementation class is associated with a container.

Public  interface Contained
{
  public  Container GetContainer ();
  Public  void Setcontainer (Container paramcontainer);
Wrapper Interface

The Org.apache.catalina.Wrapper interface represents a wrapper. A wrapper is a container that represents a separate servlet definition. The wrapper inherits the Container interface and adds several methods, and the wrapper's implementation class is responsible for managing the life cycle of its underlying servlet, including the servlet's Init,service, and the Destroy method. Because the wrapper is the lowest-level container, it is not possible to add a child container to it.
The important methods in the wrapper interface are the allocate and load methods. The allocate method is responsible for locating an instance of the servlet represented by the wrapper. The Allocate method must consider whether a servlet implements the Javax.servlet.SingleThreadModel interface, which will be discussed in Chapter 11. The Load method is responsible for the load and initialization of the servlet instance. Context Interface

A context represents a Web application in a container. A context typically contains one or more wrappers as its child containers. Important methods include methods such as Addwrapper,createwrapper.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.