【filter】springmvc web.xml

时间:2023-03-09 09:21:38
【filter】springmvc   web.xml

1、filter用于拦截用户请求,在服务器作出响应前,可以在拦截后修改request和response,这样实现很多开发者想得到的功能。

2、filter实现

×编写一个继承Filter接口的类

×在工程的web.xml文件描述此过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <filter>
  <filter-name>CharsetEcodingFilter2</filter-name>
  <filter-class>cn.itcast.filter.CharsetEcodingFilter2</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>CharsetEcodingFilter2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

3、常见用法

×处理全站中文乱码问题

×实现自动登录

×过滤敏感词汇

×压缩网页

×选择性让浏览器缓存

这几种功能的实现采用同样的原理,那就是使用包装模式或动态代理增强request或response对象的功能。