How Tomcat Works 5 under the servlet container

Source: Internet
Author: User
In the previous section, we mainly talk about wrapper containers, and in this section we talk about context containers.
Again, a context container can contain multiple wrapper containers;
A wrapper container represents an independent servlet.
Context application here we have to propose a new component-, which helps servlet container-in this section, the summary is that the context instance selects a sub-container (here is the wrapper instance) to process a specified request.
First, let's clarify the points;
The role of the er is to associate a parent container with several of its sub-containers. A parent container can have several mappers. Why? Because different protocols are supported in this way.
For example, one er is used to support the HTTP protocol, and the other er supports the HTTPS protocol (in other words, what is the difference between HTTP and HTTPS? Let's just say, that s stands for security, security, another form of confidentiality measures is adopted. Generally, banks and stock exchanges Use https)
The ER interface is as follows. We use its implementation class, simplecontextmapper class.
package org.apache.catalina;public interface Mapper {public Container getContainer();public void setContainer(Container container);public String getProtocol();public void setProtocol(String protocol);public Container map(Request request, boolean update);}


The names of each method are very good, so you should be able to understand the name.
The following is the UML class diagram of this section.


Through the description of the UML diagram, we can see that the familiar simplecontext "contains" simplecontextmapper. Now let's look at the code
public class SimpleContext implements Context, Pipeline {  public SimpleContext() {    pipeline.setBasic(new SimpleContextValve());  }  protected HashMap<String, Container> children = new HashMap<String, Container>();  protected Loader loader = null;  protected SimplePipeline pipeline = new SimplePipeline(this);  protected HashMap<String, String> servletMappings = new HashMap<String, String>();  protected Mapper mapper = null;  protected HashMap<String, Mapper> mappers = new HashMap<String, Mapper>();  private Container parent = null;  ......}


Now let me talk about these attributes;
Mapper: This is a Mapper type attribute. If a container has only one er, it is by default; if a container has several mappers (to cope with different network protocols such as HTTP and https), The Mapper is left blank;
Mappers is of the hashmap type. It contains several projectors of the container and uses the protocols supported by the Mapper as the key;
Servletmapping is also a hashmap, which stores the ing relationships between ing paths and sub-containers. For example, if the access URI is/servleta, it corresponds to the servlet of servleta;
Childen is also a hashmap, which stores the sub-container with the name of servleta (wrapper here)
The test class is as follows;
public final class Bootstrap2 {    @SuppressWarnings("deprecation")public static void main(String[] args) {    HttpConnector connector = new HttpConnector();        Wrapper wrapper1 = new SimpleWrapper();    wrapper1.setName("Primitive");    wrapper1.setServletClass("PrimitiveServlet");    Wrapper wrapper2 = new SimpleWrapper();    wrapper2.setName("Modern");    wrapper2.setServletClass("ModernServlet");    Context context = new SimpleContext();    context.addChild(wrapper1);    context.addChild(wrapper2);    Valve valve1 = new HeaderLoggerValve();    Valve valve2 = new ClientIPLoggerValve();    ((Pipeline) context).addValve(valve1);    ((Pipeline) context).addValve(valve2);    Mapper mapper = new SimpleContextMapper();    mapper.setProtocol("http");    context.addMapper(mapper);        Loader loader = new SimpleLoader();    context.setLoader(loader);    // context.addServletMapping(pattern, name);    context.addServletMapping("/Primitive", "Primitive");    context.addServletMapping("/Modern", "Modern");    connector.setContainer(context);    try {      connector.initialize();      connector.start();      // make the application wait until we press a key.      System.in.read();    }    catch (Exception e) {      e.printStackTrace();    }  }}


Let's first look at the sequence diagram.



I started directly from the base valve in the context container, that is, simplecontextvalve (the base valve in the previous section is simplewrappervalve ). The process before the basic valve is the same as that in the previous section. For details, refer to the previous section.
public void invoke(Request request, Response response, ValveContext valveContext)    throws IOException, ServletException {    .....    Context context = (Context) getContainer();    // Select the Wrapper to be used for this Request    Wrapper wrapper = null;    try {      wrapper = (Wrapper) context.map(request, true);                }    catch (IllegalArgumentException e) {      badRequest(requestURI, (HttpServletResponse) response.getResponse());      return;    }    if (wrapper == null) {      notFound(requestURI, (HttpServletResponse) response.getResponse());      return;    }    // Ask this Wrapper to process this Request    response.setContext(context);    wrapper.invoke(request, response);  }


OK. Let's take a look at the map method of simplecontext;
  public Container map(Request request, boolean update) {    //this method is taken from the map method in org.apache.cataline.core.ContainerBase    //the findMapper method always returns the default mapper, if any, regardless the    //request's protocol    Mapper mapper = findMapper(request.getRequest().getProtocol());    if (mapper == null)      return (null);    // Use this Mapper to perform this mapping    return (mapper.map(request, update));  }  public Mapper findMapper(String protocol) {    // the default mapper will always be returned, if any,    // regardless the value of protocol    if (mapper != null)      return (mapper);    else      synchronized (mappers) {        return ((Mapper) mappers.get(protocol));      }  }


The findmapper method always returns the default Er (as we mentioned earlier, if there is only one ER in the container, it is the default. If there are multiple, the default property of the storage er is null.) If multiple mappers exist, find them in mappers according to the network protocol;
After finding the er, it's easy.
Wrapper. Invoke (request, response );
This goes back to the section above, calling all wrapper valves until the base valves (in this section, the wrapper pipeline has only the base valves, the first two valves, in the pipeline of the context), enter wrapper, and call allocate .......

How Tomcat Works 5 under the servlet container

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.