title: Spring MVC Execution Order: Filter and Interceptor
body: <p>I'm developing a spring MVC application that uses a filter implementing <a href="https://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html" rel="noreferrer"><code>javax.servlet.Filter</code></a> and an Interceptor extending <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/handler/HandlerInterceptorAdapter.html" rel="noreferrer">org.springframework.web.servlet.handler.HandlerInterceptorAdapter</a>.</p>
  
  <p>As far as I know, the control flow of Spring MVC is something like this:</p>
  
  <ol>
  <li>Request arrives at <code>DispatcherServlet</code>.</li>
  <li><code>DispatcherServlet</code> sends it to <code>Interceptor</code> and the overrided <code>preHandle</code> method is executed.</li>
  <li>Request arrives at the matching Controller.</li>
  <li>After processing the request, if <code>postHandle</code> method of interceptor is also overrided Spring executes its code.</li>
  <li><code>DispatcherServlet</code> uses the view resolver and sends the model to view, rendering it.</li>
  </ol>
  
  <p>Doing some tests I can see my filter is always executed BEFORE the <code>preHandle</code> method. It seems to be the first executed thing after <code>DispatcherServlet</code>. It's ok, but I do not find the reason for this behaviour. Someone with a good explanation?</p>
  
  <p>Thanks! </p>
  
  <p>UPDATE: Possibility: It's because filter is defined in <code>web.xml</code> (like <code>DispatcherServlet</code>) so Filter is executed before <code>DispatcherServlet</code>?</p>