Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Interceptors and Filters - Is this right?

Tags:

struts2

struts

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?

like image 242
user447607 Avatar asked Apr 07 '12 19:04

user447607


People also ask

Are interceptors and filters different?

Filters and interceptors can be used on both sides, on the client and the server side. Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams.

What is difference between filter and interceptor in spring boot?

Filter is related to the Servlet API and HandlerIntercepter is a Spring specific concept. Interceptors will only execute after Filters. Fine-grained pre-processing tasks are suitable for HandlerInterceptors (authorization checks, etc.)

What is the use of interceptors?

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.

What are filters in struts?

In the web. xml file, Struts defines its FilterDispatcher, the Servlet Filter class that initializes the Struts framework and handles all requests. This filter can contain initialization parameters that affect what, if any, additional configuration files are loaded and how the framework should behave.


1 Answers

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.

like image 111
graphics123 Avatar answered Oct 01 '22 13:10

graphics123