Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of javax.servlet.FilterChain? [duplicate]

I inherited a Struts 1 app that heavily utilizes FilterChain and I don't understand the benefit of this extremely obfuscating code.

"In the Servlet API, you normally use a Servlet when you want to control, preprocess and/or postprocess specific requests. But when you want to filter/modify common requests and/or responses based on specific conditions, then a Filter is much more suitable."

Every request in my app is based on specific conditions, e.g., a merchant id or a search term. But it seems like placing a request inside a whole chain of stuff that completely hides what is going on from the developer trying to trace the cause of an error, is nuts.

like image 881
barclay Avatar asked Feb 04 '13 18:02

barclay


2 Answers

The FilterChain#doFilter() call just continues the HTTP request to the destination, following exactly the same path as if you didn't use a filter in first place. This is usually a servlet class or even a JSP file. So, in order to debug problematic code, better put a breakpoint in the target code, not in the filter if it doesn't contain any code of interest.

like image 190
BalusC Avatar answered Oct 04 '22 03:10

BalusC


My coworker (who's not registered on SO) explained that it's for applying global functionality to an app that you don't want to do in every single controller, such as checking if the user is logged in.

like image 24
barclay Avatar answered Oct 04 '22 02:10

barclay