In a Filter.doFilter
method I made this call chain.doFilter
.
What is doFilter
doing inside a doFilter
? Isn't it a recursive call?
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.
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.
Three methods – init(), doFilter(), destroy().
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With