Tomcat principle and implementation-----Tomcat filter (third article)

Source: Internet
Author: User
Tags wrapper

For Tomcat filters, everyone should be familiar, whenever an HTTP request arrives, passes through the filter, we know that the filter will take precedence over the servlet execution, And when we use it, we just configure a filter tag in the Web.xml file, what exactly is he in Tomcat? Let's take a look at it today.

Finally attach the source code for your reference:

(1) Startup settings for the Bootstrap class:

<span style= "White-space:pre" >		</span>httpconnector connector = new HttpConnector ();
		<span style= "Background-color:rgb (102, 255, 255);" >wrapper wrapper = new Simplewrapper ();</span>
		wrapper.setservletclass ("Modernservlet");
		Loader Loader = new Simpleloader ();
		Valve valve1 = new Headerloggervalve ();   ---this is equivalent to a filter
		Valve valve2 = new Clientiploggervalve ();

		Wrapper.setloader (loader);
		((Pipeline) wrapper). Addvalve (valve1);
		((Pipeline) wrapper). Addvalve (valve2);

		Connector.setcontainer (wrapper);
(2) The AddValue () method, and finally the value is added to the object array of value. The specific code references are as follows

This is in the simplewrapper of the bootstrap, see below:

<span style= "White-space:pre" >	</span>public synchronized void Addvalve (Valve Valve) {
		Pipeline.addvalve (valve);
	<span style= "White-space:pre" >	</span>}
is an addition that is implemented by calling the pipeline AddValue () method. And then we're going to take a closer look at the Pipline class Simplepipeline:

Import java.io.IOException;
Import javax.servlet.ServletException;
Import org.apache.catalina.Contained;
Import Org.apache.catalina.Container;
Import Org.apache.catalina.Pipeline;
Import Org.apache.catalina.Request;
Import Org.apache.catalina.Response;
Import Org.apache.catalina.Valve;

Import Org.apache.catalina.ValveContext; public class Simplepipeline implements Pipeline {public simplepipeline (Container Container) {Setcontainer (Container)
	;
	}//The basic Valve (if any) associated with this Pipeline.
	protected Valve basic = null;
	The Container with which this Pipeline is associated.
	protected Container Container = null;

	The array of valves protected Valve valves[] = new Valve[0];
	public void Setcontainer (Container Container) {this.container = Container;
	Public Valve Getbasic () {return basic;
		public void Setbasic (Valve Valve) {this.basic = Valve;
	((contained) valve). Setcontainer (container); } public void Addvalve (Valve Valve) {if (Valve Instanceof contained) ((contained) valve). Setcontainer (This.container);
			Synchronized (valves) {Valve results[] = new Valve[valves.length + 1];
			System.arraycopy (valves, 0, results, 0, valves.length);
			Results[valves.length] = valve;
		valves = results;
	} public valve[] Getvalves () {return valves;  public void invoke (Request request, Response Response) throws IOException, Servletexception {//Invoke the
	Valve in the pipeline for this request (new Simplepipelinevalvecontext ()). Invokenext (request, response);  } public void Removevalve (Valve Valve) {}//This class is copied from Org.apache.catalina.core.StandardPipeline//
	Class ' s//Standardpipelinevalvecontext inner class.

		Protected class Simplepipelinevalvecontext implements Valvecontext {protected int stage = 0;
		Public String GetInfo () {return null; public void Invokenext (Request request, Response Response) throws IOException, servletexception {int SUBSCRIPT = stage;
			stage = stage + 1; Invoke the requested Valve for the "current request thread if (Subscript < valves.length) {Valves[subscript].
			Invoke (Request, response, this);
			else if ((subscript = = valves.length) && (basic!= null)) {Basic.invoke (Request, response, this);
			else {throw new servletexception ("No valve"); }}//End of inner class}
The chain of filtration is formed in this class.

In Simplepipeline, the AddValue () method adds the previously mentioned filter to the object array of value, in Simplepipelinecontext in its inner class, Invokenext () The iterative invocation of the filter chain is implemented, of course, and this iterative invocation begins with the creation of the Httpprocessor: Connector.getcontainer (). Invoke (request, response);

Because the connector.setcontainer (wrapper) has been set in the bootstrap; So the Invoke () method in the Simplewrapper is called:

Note: The filter chain can be implemented in order to modify the data----"" This time the request and response object will be passed into the next invoke method, but if a filter is abnormal, then the filter chain will not be completed, and finally to what the Err page. It's the same thing with Struts2 interceptors, and in struts he uses a/x filter to intercept all requests and then freely perform the processing of the data.

public void invoke (Request request, Response Response) throws IOException,
			servletexception {
		Pipeline.invoke ( request, response);
	}
In Pipline or called:

public void invoke (Request request, Response Response) throws IOException,
servletexception {
Invoke the ' the ' the ' Valve in this ' pipeline for this request
(New Simplepipelinevalvecontext ()). Invokenext (request, response);
}
Because the filter (value array) of all filter chains already exists in Simplepipeline, in Invokenext, the invocation is done by means of a cursor.

Connection: http://download.csdn.net/detail/hao707822882/7484611 free download


In a filter, then we should think about the life cycle of the 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.