Tomcat Core Components container container-related

Source: Internet
Author: User
Tags bind error handling throwable wrapper tomcat

Preface engine Container host container

Preface

Connector the socket that encapsulates the request object and the response object is passed to the container container, what is the processing flow in the Contianer container? Before saying container container, it is necessary to have a simple understanding of the container container, container container is the parent interface of the child container, all the sub-containers must implement this interface, in Tomcat container container design is a typical responsibility chain design pattern, It has four sub-containers: Engine, Host, context, and wrapper. The four containers are a parent-child relationship, and the engine container contains host,host containing context,context containing wrapper.

We have a servlet class in the Web project corresponding to a wrapper, multiple servlets corresponding to multiple wrapper, when there are multiple wrapper need a container to manage these wrapper, this is the context container, The context container corresponds to a project, so we will create a new context container in the new deployment of a project into Tomcat. The processing of container containers is also complex, and here is an approximate flow:

The above appeared pipeline and Valve, the two objects can be understood as pipelines and pipelines in the gate, when received from the connector request, the request to pass through a pipeline and the gate of a pipeline, only the entire pass can eventually be the specific servlet processing. It is important to note that each container has its own pipes and gates, which are controlled by the container itself, so we can see the injected Standardenginevalve class.

The following is an analysis of how each container is handled, starting with the four sub-containers of the container container: engine container

Engine container contains the host container, according to the first part of the article's architecture diagram, you can know that its management container is Host,engine is an interface, its standard implementation class is Standardengine, the following is its class structure diagram:

Note that the Addchild method, whose type is container, is actually managed by the host container. The process of handling requests by the engine container can be simplified as follows:

In the beginning of the flowchart called the Standardenginevalve Invoke method, the implementation of this method is how to do it.

    /** * Select the appropriate child Host to process this request, * based on the requested server name.
     If No matching Host can * is found, return an appropriate HTTP error. * * @param request request to being processed * @param response response to be produced * * @exception IO Exception If an input/output error occurred * @exception servletexception if a servlet error occurred */@Ov
        Erride Public final void invoke (Request request, Response Response) throws IOException, Servletexception {
        Select the host to is used for this Request host host = Request.gethost (); if (host = = null) {Response.senderror (httpservletresponse.sc_bad_request, S
            M.getstring ("Standardengine.nohost", Request.getservername ()));
        Return } if (request.isasyncsupported ()) {request.setasyncsupported (HOST.GETPIPeline (). isasyncsupported ());
    }//Ask This Host to process this request Host.getpipeline (). GetFirst (). Invoke (request, response); }

You can see that the task of this method is to select the available host container to process the current request, select the host container, call its Invoke method, so the specific processing is transferred to the host container. host Container

The host container is a sub-container of the engine container, which also says that host is managed by the engine container, refers to a virtual host, for example, we visit the specific JSP page URL localhost is a virtual host, its role is to run multiple applications, and management of these applications , its sub-container is the context, and a host also holds information about the host. The standard implementation class of host is Standardhost, its gate implementation is standardhostvalve, the following is the class structure diagram of Standardhost and standardhostvalve:

The processing flow of the host container can be simplified as follows:

Then we go back to the engine container's Invoke method, below is Host.getpipeline (). GetFirst (). Invoke (Request, response) method source code:

    @Override Public final void invoke (Request request, Response Response) throws IOException, Servletexceptio
        n {//Select the context to is used for this Request Context context = Request.getcontext ();
                 if (context = = null) {Response.senderror (Httpservletresponse.sc_internal_server_error,
            Sm.getstring ("Standardhost.nocontext"));
        Return }//Bind the context CL to the current thread if (context.getloader () = null) {//Not star
            Ted-it should check for availability first//This should eventually move to Engine, it ' s generic.
                        if (globals.is_security_enabled) {privilegedaction<void> pa = new PRIVILEGEDSETTCCL (
                Context.getloader (). getClassLoader ());                
            Accesscontroller.doprivileged (PA); } else {Thread.CurrentThread (). SETCONTEXTCLAssloader (Context.getloader (). getClassLoader ()); }} if (Request.isasyncsupported ()) {request.setasyncsupported (Context.getpipeline (). Isasyncs
        Upported ()); }//Don ' t fire listeners during async processing//IF a request init listener throws an exception, the R 
        Equest is//aborted Boolean asyncatstart = Request.isasync (); An async error page could dispatch to another resource. This flag helps//ensure a infinite error handling loop is not entered Boolean Erroratstart = response.i
        Serror ();
            if (Asyncatstart | | context.firerequestinitevent (REQUEST)) {//Ask this context to process the this request
            try {context.getpipeline (). GetFirst (). Invoke (request, response);
                } catch (Throwable t) {exceptionutils.handlethrowable (t); if (erroratstart) {container.GetLogger (). Error ("Exception processing" + Request.getrequesturi (), t);
                    } else {Request.setattribute (requestdispatcher.error_exception, T);
                Throwable (Request, response, T); }}/If the request is async at the start and a error occurred then//the Async Error handling would kick-in and that would fire the/request destroyed event *after* the error handling have T Aken//Place if (! ( Request.isasync () | | (Asyncatstart && Request.getattribute (requestdispatcher.error_exc Eption) = null)) {//Protect against NPEs if context was destroyed during a//long run
                Ning request. if (Context.getstate (). IsAvailable ()) {if (!erroratstart) {//Error page Pro
            Cessing            Response.setsuspended (FALSE);

                        Throwable t = (throwable) request.getattribute (requestdispatcher.error_exception);
                        if (t! = null) {Throwable (request, response, T);
                        } else {status (request, response);
                }} context.firerequestdestroyevent (Request); }}}//Access a session (if present) to update last accessed time, based on a//STR
        ICT interpretation of the specification if (access_session) {request.getsession (false); }//Restore the context ClassLoader if (globals.is_security_enabled) {Privilegedaction<vo
            id> pa = new PRIVILEGEDSETTCCL (StandardHostValve.class.getClassLoader ()); Accesscontroller.doprivilegeD (PA); } else {Thread.CurrentThread (). Setcontextclassloader (StandardHostValve.class.getClassLoa
        Der ()); }
    }

The process can be summarized as follows: Select a context container for a specific request URL to bind the context container to a thread to determine whether it is an asynchronous request to the context to process the request context to execute the Invoke method, into the pipeline, Handled by Standardcontextvalve (the standard implementation class of the Contextvalve)


Original blog owner: Rhwayfunn

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.