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:
Does this seem like an accurate and complete answer? Should I add or correct anything? What about threading issues / differences?
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.
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.)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With