5

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.

matt b
  • 138,234
  • 66
  • 282
  • 345
barclay
  • 4,362
  • 9
  • 48
  • 68
  • 4
    Have you read this? http://www.oracle.com/technetwork/java/filters-137243.html – matt b Feb 04 '13 at 18:58
  • Are you asking hat a FilterChain is used for, or why it is used in your app. If the former, then read the documentation. If the latter, how could we answer without knowing anything about your app? – JB Nizet Feb 04 '13 at 19:07
  • 2
    +1; in general I agree with you, a Filter hides a number of details. In fact it's just an abstraction helping you in designing your application (of course it's becoming more interesting in case you build a platform like a servlet engine). My personal option is that you should *not* place any kind of business logic in a filter (like merchant). Implement certain non-functional aspects like logging, authentication, etc. in a filter. – home Feb 04 '13 at 19:20
  • @home thanks for being helpful and understanding the purpose of my question. – barclay Feb 04 '13 at 19:27

2 Answers2

8

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I would love to, except I cannot repro this error locally, only in my prod environment. Thanks though. – barclay Feb 04 '13 at 19:28
7

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.

barclay
  • 4,362
  • 9
  • 48
  • 68