欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet中的filter对servlet过滤的执行顺序

程序员文章站 2022-07-02 17:53:13
...

在配置web工程中的web.xml文件时,filter可以过滤servlet,那对同一个servlet配置多个filter时,执行顺序是怎样的呢?看如下配置:

<filter>
  	<filter-name>CheckLoginFilter</filter-name>
  	<filter-class>web.CheckLoginFilter</filter-class>
 </filter>
 <filter-mapping>
        <filter-name>CheckLoginFilter</filter-name>
        <servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>

<filter>
	<filter-name>Set Character Encoding</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
</filter>
<filter-mapping>
	<filter-name>Set Character Encoding</filter-name>
	<servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>

<filter>
	<filter-name>httpPutFormFilter</filter-name>
	<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>httpPutFormFilter</filter-name>
	<servlet-name>mvc-dispatcher</servlet-name>
</filter-mapping>

<servlet>
	<servlet-name>mvc-dispatcher</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>mvc-dispatcher</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>
 

 filter的执行顺序是根据filter-mapping配置的先后执行的。按照上面的配置,第一个filter执行是CheckLoginFilter。那么servlet什么时候执行呢?

那要看一下webx中配置的执行顺序:listener->filter->servlet


所以上面的配置执行顺序是:
CheckLoginFilter->Set Character Encoding->httpPutFormFilter->mvc-dispatcher。

 

 

相关标签: sevlet filter