Emilio
Emilio

Reputation: 1044

Spring MVC Execution Order: Filter and Interceptor

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:

  1. Request arrives at DispatcherServlet.
  2. DispatcherServlet sends it to Interceptor and the overrided preHandle method is executed.
  3. Request arrives at the matching Controller.
  4. After processing the request, if postHandle method of interceptor is also overrided Spring executes its code.
  5. 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?

Upvotes: 17

Views: 14673

Answers (1)

Santosh Joshi
Santosh Joshi

Reputation: 3320

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.

Upvotes: 18

Related Questions