How Tomcat works reading notes 11 standwrapper

Source: Internet
Author: User

Standardwrappervalvestandardwrappervalve is the base valve of the Standardwrapper, which has completed three main tasks.
1 Call Standardwrapper's allocate method to get the servlet instance represented by the Standardwrapper
2 perform all filters associated with the Servelt
3 Calling the Servlet's service method

The 23rd step can be subdivided into
Call its private Createfilterchain method to get the filter chain
Invokes the DoFilter method of the filter chain. In this case, the Servlet's service method is called
Release the filter chain
Deallocate method for calling wrappers

If the servlet cannot be used, call the wrapper's Unload method


Let's take a look at the approximate code snippet
Allocate a servlet instance to process this requesttry {if (!unavailable) {servlet = Wrapper.allocate (); }}...//Acknowlege the requesttry {response.sendacknowledgement ();} ...//Create the filter chain for this request Applicationfilterchain Filterchain = Createfilterchain (Request,servlet); Call the filter chain-request//This also calls the servlet ' s Servicet () methodtry {String jspfile = Wrapp    Er.getjspfile ();    if (jspfile! = null) Sreq.setattribute (globals.jsp_file_attr, jspfile);    else Sreq.removeattribute (globals.jsp_file_attr);    if (servlet = null) && (Filterchain! = null)) {Filterchain.dofilter (sreq, sres); } sreq.removeattribute (globals.jsp_file_attr);} ...//Release the filter chain (if any) for this requesttry {if (filterchain! = null) filterchain.release ();}. ..    Deallocate the allocated servlet instancetry {if (servlet! = null) {wrapper.deallocate (servlet); }}...//If this servlet has been marked permanently unavailable,//unload it and release this instancetry {if (servlet! = null) && (WRA    Pper.getavailable () ==long.max_value) {wrapper.unload (); }}


Look at the code above, you should be able to see that the most complex part has two
One applicationfilterchain Filterchain = Createfilterchain (Request,servlet);
Secondly Filterchain.dofilter (Sreq, sres);
Step-by-step.

The full name of the class Filterdef class should be filterdefinition, the filter description class.
It contains basic information such as filter's Filterclass,filtername, and Get/set method.
Here's the property that we can look at this
    /**     * The set of initialization parameters for this filter, keyed by     * parameter name.     *    /private map<string, string> parameters = new hashmap<string, string> ();
The initialization parameter is stored with HASHMAP, it has a get method, and the method of adding the property is Addinitparameter (string name, String value).

The Applicationfilterconfig class Org.apache.catalina.core.ApplicationFilterConfig implements the Javax.servlet.FilterConfig interface. Applicationfilterconfig is responsible for managing the filter instances that were created when the Web application was launched.
Its constructor is as follows:
Public Applicationfilterconfig (context context, filterdef filterdef)
Throws ClassCastException, Classnotfoundexception,illegalaccessexception, Instantiationexception, ServletException
Here we mainly talk about its GetFilter method, the function of this method is actually loading the filter class and initializing it.
First from the filterdef inside get filterclass;
        String Filterclass = Filterdef.getfilterclass ();        ClassLoader ClassLoader = null;        if (Filterclass.startswith ("Org.apache.catalina."))            ClassLoader = This.getclass (). getClassLoader ();        else            ClassLoader = Context.getloader (). getClassLoader ();    .....            Class<?> clazz = Classloader.loadclass (filterclass);        This.filter = (filter) clazz.newinstance ();        Filter.init (this);        return (This.filter);
There is still nothing to say.


The Invoke method in the Applicationfilterchain class Standardwrappervalve class creates an instance of the class and calls its DoFilter method. The DoFilter (in fact, internaldofilter) of the Applicationfilterchain class invokes the DoFilter method of the first filter in the chain.
In the Applicationfilterchain class, there is a reference to the Applicationfilterconfig

Private arraylist<applicationfilterconfig> filters = new arraylist<applicationfilterconfig> ();

See the put, array form to store the chain.

A typical chain of responsibility model.

The signature of the Dofilter method in the Filter interface is as follows:
public void DoFilter (Servletraquest request, Servletresponse Response,filterchain chain)            throws Java.io.IOException, servletexception in Applicationfilterchain's Dofilter method, it passes itself as a third parameter to it. Let's look at an example of a filter implementation class public void DoFilter (ServletRequest request, servletresponse response,                    Filterchain chain) throws IOException, Servletexception {    //do something    here ... Chain.dofilter (request, response);}
It's repeating.
The loop here is not so easy to understand, it is recommended to refer to my other blog

http://blog.csdn.net/dlf123321/article/details/40078583


When all the filter is executed, the following code

           if (Request instanceof HttpServletRequest) &&                (response instanceof HttpServletResponse)) {                Servlet.service ((httpservletrequest) request,                                (httpservletresponse) response);            } else {                Servlet.service (request, response);            }

When is the filter done?

Private iterator<applicationfilterconfig> Iterator = null, ... if (this.iterator = = null)            This.iterator = Filters.iterator ();        Call the next filter if there is one        if (This.iterator.hasNext ()) {                    //execute filter       } ...      Calling the code block of the servlet's service ...


There is no difference between the application and the previous chapters, not to repeat it.






How Tomcat works reading notes 11 standwrapper

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.