I'm developing a spring MVC application that uses a filter implementing javax.servlet.Filter
and an Interceptor extending org.springframework.web.servlet.handler.HandlerInterceptorAdapter.
As far as I know, the control flow of Spring MVC is something like this:
DispatcherServlet
.DispatcherServlet
sends it to Interceptor
and the overrided preHandle
method is executed.postHandle
method of interceptor is also overrided Spring executes its code.DispatcherServlet
uses the view resolver and sends the model to view, rendering it.Doing some tests I can see my filter is always executed BEFORE the preHandle
method. It seems to be the first executed thing after DispatcherServlet
. It's ok, but I do not find the reason for this behaviour. Someone with a good explanation?
Thanks!
UPDATE: Possibility: It's because filter is defined in web.xml
(like DispatcherServlet
) so Filter is executed before DispatcherServlet
?
Interceptors will only execute after Filters. Fine-grained pre-processing tasks are suitable for HandlerInterceptors (authorization checks, etc.)
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. You can use interceptors for example to zip and unzip output and input entity streams.
postHandle() : After a request is handled by a request handler. It gives access to the returned ModelAndView object, so you can manipulate the model attributes in it. afterCompletion() : After the completion of all request processing i.e. after the view has been rendered.
It's perfectly fine as
Filter's
are part of Servlet specification.
Filters
are called by your Server(tomcat)
. while Interceptors are called by Spring
.
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