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

Internally it invokes doFilter of the next filter in the filter chain, and, when chain is over, it invokes the target servlet.


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.