Difference between Interceptors and Filters - Is this right?
Asked Answered
L

4

13

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?

Lotuseater answered 7/4, 2012 at 19:7 Comment(0)
A
5

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.

Assessment answered 9/4, 2012 at 20:29 Comment(2)
i would like to hear the answers for above 3 question you posted please?Baluchi
@Baluchi I suggest that you ask them as independent questions. It would be a confusing format to answer them in this question.Assessment
I
3

Filters are from Servlet API and Interceptors are from Struts 2 , Difference comes when we talk about web applications and enterprise apps, filter is used only in web applications whereas interceptor can be used with web as well as enterprise applications. Life cycle methods of both, also differs.

The interceptor stack fires on every request. filters only apply to the urls for which they are defined. you use one or the other depending on need. Lets say you need to verify a cookie is present for every request. Use an interceptor. Lets say that you need to pop up an external app on some requests (driven by a url), use a filter.

Immersed answered 20/2, 2014 at 11:35 Comment(1)
"filter is used only in web applications whereas interceptor can be used with web as well as enterprise applications"? This is misleading; there's nothing magic about "enterprise apps", they are just a subset of all web apps. Also a filter can easily be applied to all URLs, so that's not a substantive difference.Lifelong
G
1

In one of the interview I answered this question like,

Interceptors are struts2 concept and application developer has control over it. Also, It works withing struts2 application boundary and it has access to Actions, ValueStack and other objects along with Struts2 goodies.

While Filter is more suitable for deployment time configurations. It has limited access to application, And in most cases its generic for most of the web-app deployed in server.

If logic in code is more coupled with Application, its better suited for interceptor or else if its infrastructure, we can place it in servlet filter.

Giorgia answered 11/4, 2012 at 16:59 Comment(0)
C
0

Filters:

  1. Based on Servlet Specification
  2. Executes on the pattern matches on the request.
  3. Not configurable method calls.

Interceptors:

  1. Based on Struts2.
  2. Executes for all the request qualifies for a front controller (a Servlet filter). And can be configured to execute additional interceptors for a particular action execution.
  3. Methods in the Interceptors can be configured whether to execute or not by means of excludeMethods or includeMethods.
Copulate answered 28/9, 2016 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.