Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How filter chain invocation works?

I am trying to understand filter chaining.As defined in this question

All filters are chained (in the order of their definition in web.xml). The chain.doFilter() is proceeding to the next element in the chain. The last element of the chain is the target resource/servlet.

I am interested to know behind the scene in container that how container handles filter chaining.Can someone explain that how Filter chaining is handled inside container?

like image 543
Abhijeet Panwar Avatar asked Aug 08 '14 05:08

Abhijeet Panwar


People also ask

How do spring boot filter chains work?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.

What is the role of doFilter () method?

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

How does a servlet filter work?

How does Servlet Filter work? When a request is made, it reaches the Web Container and checks if the filter contains any URL pattern, which is similar to the pattern of the URL requested. The Web Container locates the very first filter which matches the request URL, and then that filter code is executed.

What is the purpose of Tomcat filter?

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method.


1 Answers

Each filter implements the javax.servlet.Filter interface, which includes a doFilter() method that takes as input a request and response pair along with a filter chain, which is an instance of a class (provided by the servlet container) that implements the javax.servlet.FilterChain interface. The filter chain reflects the order of the filters. The servlet container, based on the configuration order in the web.xml file, constructs the chain of filters for any servlet or other resource that has filters mapped to it. For each filter in the chain, the filter chain object passed to it represents the remaining filters to be called, in order, followed by the target servlet.

If there are two filters, for example, the key steps of this mechanism would be as follows:

enter image description here

1.The target servlet is requested. The container detects that there are two filters and creates the filter chain.

2.The first filter in the chain is invoked by its doFilter() method.

3.The first filter completes any preprocessing, then calls the doFilter() method of the filter chain. This results in the second filter being invoked by its doFilter() method.

4.The second filter completes any preprocessing, then calls the doFilter() method of the filter chain. This results in the target servlet being invoked by its service() method.

5.When the target servlet is finished, the chain doFilter() call in the second filter returns, and the second filter can do any postprocessing.

6.When the second filter is finished, the chain doFilter() call in the first filter returns, and the first filter can do any postprocessing.

7.When the first filter is finished, execution is complete.

Filters can be interposed between servlets and the servlet container to wrap and preprocess requests or to wrap and postprocess responses. None of the filters are aware of their order. Ordering is handled entirely through the filter chain, according to the order in which filters are configured in web.xml

like image 198
SparkOn Avatar answered Oct 21 '22 11:10

SparkOn