user2468453
user2468453

Reputation: 21

Does a DispatchServlet intercept an HTTP request before a filter does?

In our web.xml, we have the following lines:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
       <init-param>
          <param-name>contextAttribute</param-name>
          <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
       <init-param>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
<filter-mapping>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/<url-pattern>
<servlet-mapping>

The spring-servlet.xml file defines an element for filter configuration.

My question is: When an http request is sent to this application, which entity (DispatcherServlet or filter) will see the request first? Would the filter does its work on the request and hand it over to the DispatcherServlet? Or would the DispatcherServlet accept the request and provide it to the filter?

Thank you very much for your help.

Upvotes: 2

Views: 382

Answers (1)

AllTooSir
AllTooSir

Reputation: 49382

Filters are always called before Servlets. When you have filters and a servlet matching the URL pattern, all filters are executed first in the order of <filter-mapping> definitions and then the request and response is passed to the Servlet. From the Spring MVC perspective , what you are looking for is the HandlerInceptor.

Upvotes: 3

Related Questions