过滤器Filter
它依赖于servlet容器。在实现上,基于函数回调,它可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次
通常用的场景是:在过滤器中修改字符编码(CharacterEncodingFilter)、在过滤器中修改HttpServletRequest的一些参数
使用SpringMVC则就只需要在web.xml中配置过滤器即可,当然前提需要支持Maven所对应的jar
1.在pom.xml中导入jar
一般搭建SSM项目时候,都会导入spring框架所需的jar ,比如spring-core、spring-web等等
<!-- SpringMVC过滤器在spring-web中 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
2.在web.xml中进行配置
以较为常用的编码过滤器为例,进行web.xml配置
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter