Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is chain.doFilter doing in Filter.doFilter method?

In a Filter.doFilter method I made this call chain.doFilter.

What is doFilter doing inside a doFilter? Isn't it a recursive call?

like image 609
giri Avatar asked Jan 13 '10 15:01

giri


People also ask

What does Chain doFilter do?

doFilter. Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.

What do you mean by filter chaining?

A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource.

Which three methods are used to generate filters?

Three methods – init(), doFilter(), destroy().

When init () method of filter gets called?

Q 20 - When init method of filter gets called? A - The init method is called by the web container to indicate to a filter that it is being placed into service.


2 Answers

Servlet Filters are an implementation of the Chain of responsibility design pattern.

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.

like image 146
Bozho Avatar answered Oct 09 '22 02:10

Bozho


It is calling the doFilter method of the chain object, not itself, so no, it won't be recursive.

The name chain suggests that you have a sequence of filters, with each filter doing some processing and then passing on to the next in sequence, so each object has a chain member to point to the next filter in the sequence, which gets called after the filter has performed its own processing. The last in the sequence will then probably have null as the chain value, or it knows on its own that it is the last one in the sequence.

like image 36
JaakkoK Avatar answered Oct 09 '22 02:10

JaakkoK