Difference between Interceptors and Filters - Is this right?

user447607 picture user447607 · Apr 7, 2012 · Viewed 28.4k times · Source

I'm researching this so that I can respond better in interviews. I've been searching around for a clear and concise answer.

So far, and by all means correct me if I am wrong or lacking in detail:

  1. Filters are part of the Servlet API, Interceptors are Struts 2. (Seems obvious though)
  2. The Interceptor stack fires on requests in a configured package while filters only apply to their mapped URLs.
  3. Interceptors can be configured to execute or not depending on specific target action methods via excludeMethods and includeMethods while Filters lack this feature.
  4. Filters are an implementation of the Intercepting Filter pattern while Interceptors are of the Interceptor pattern.

Does this seem like an accurate and complete answer? Should I add or correct anything? What about threading issues / differences?

Answer

chad picture chad · Apr 9, 2012

I think the first point is the answer, but you should probably be able to say more than just

interceptors are in struts 2 and filters are part of the Servlet API

In fact, as an interviewer, I would be expecting and hoping to here you explain exactly what the difference between the Servelt API and a web application framework, like Struts 2 is. This is actually a chance to show your understanding of the entire Java web app ecosystem and infrastructure.

Good answers might touch upon:

  1. How the Struts 2 framework is implemented within a Servlet Filter.

  2. What specific use cases would call for a servlet filter outside of struts 2 but within the same web application?

  3. What indeed is the Servlet API, a web application, a web application framework, etc.?

Answering in way that showed your understanding of all of these topics is what there looking for, I would guess.