Filter被称为过滤器或者拦截器其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应后实现一些特殊功能。
Filter 的过滤器就是实现了一个javax.servlet.Filter接口的类,在javax.servlet.Filter接口中定义了三个方法
init(FilterConfig filterConfig)初始化过滤器
doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
destory();
生命周期:
诞生:过滤器的实例是在应用被加载时就完成的实例化,并初始化的。
存活:和应用的生命周期一致的。在内存中是单例的。针对拦截范围内的资源访问,每次访问都会调用void doFIlter(request,response.chain)进行拦截。
死亡:应用被卸载。
拦截器的使用实现Filter,然后web.xml中配置
Filter映射
1.使用“*”通配符拦截所有
2.拦截不同方式的访问请求
web.xml文件中,一个<filter-mapping>元素用于配置一个Filter所负责拦截的资源
<filter-mapping>元素有一个特殊 的子元素<dispatcher>用于指定过滤器所拦截的资源被
Servlet容器调用的方式。
<filter>
<filter-name>qq3</filter-name>
<filter-class>Demo1</filter-class>
</filter>
<filter-mapping>
<filter-name>qq3</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
1.request
直接访问页面会过滤
如果目标资源是通过RequestDispatch的include()或者forword()方法访问时该过滤器不会被调用
2.include
如果目标资源是通过RequestDispatch的include()访问时会调用
3.forword
如果目标资源是通过RequestDispatch的forword()方法访问时会调用
4.error
异常处理机制会处理
Filter链(串联过滤器)
多个Filter程序对同一个URL进行拦截这些Filter就会组成一个Filter链(也叫过滤器链)
Filter链用FilterChain对象来表示,FilterChain中有一个doFilter()方法,作用就是让
Filter链上的当前过滤器放行,请求进入下一个Filter;
一个过滤器接着另外一个过滤器。执行的顺序
<filter-mapping>的先后顺序决定
过滤器解决乱码
//解决post方式请求参数和响应编码问题的过滤器
public class SetCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){//忘记配置该参数
encoding = "UTF-8";//默认编码
request.setCharacterEncoding(encoding);//只能解决POST请求参数的中文问题
response.setCharacterEncoding(encoding);//输出流编码
response.setContentType("text/html;charset="+encoding);//输出流编码,通知了客户端应该使用的编码
chain.doFilter(request, response);
}
--------------------------------------------------------------------------
<filter>
<filter-name>qq3</filter-name>
<filter-class>Demo1</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>qq3</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
----------------------------------------------------------------------
public class SetCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encoding = filterConfig.getInitParameter("encoding");
request.setCharacterEncoding(encoding);//只能解决POST请求参数的中文问题
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
控制动态资源不缓存
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// HttpServletRequest request = (HttpServletRequest)req;
// HttpServletResponse response = (HttpServletResponse)resp;
HttpServletRequest request = null;
HttpServletResponse response = null;
try{
request = (HttpServletRequest)req;
response = (HttpServletResponse)resp;
}catch(Exception e){
throw new RuntimeException("not-http request or response");
}
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
chain.doFilter(request, response);
}
控制静态资源缓存时间
<init-param>
<param-name>html</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>css</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>js</param-name>
<param-value>3</param-value>
</init-param>
--------------------------------------------
public class StaticResourcesNeedCacheFilter implements Filter {
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = null;
HttpServletResponse response = null;
try{
request = (HttpServletRequest)req;
response = (HttpServletResponse)resp;
}catch(Exception e){
throw new RuntimeException("not-http request or response");
}
long time = 0;//缓存的时间
//根据用户请求的uri地址的后缀:
String uri = request.getRequestURI();
String exName = uri.substring(uri.lastIndexOf(".")+1);
if("html".equals(exName)){
String value = filterConfig.getInitParameter("html");//小时
time = Long.parseLong(value)*60*60*1000;
}
if("css".equals(exName)){
String value = filterConfig.getInitParameter("css");//小时
time = Long.parseLong(value)*60*60*1000;
}
if("js".equals(exName)){
String value = filterConfig.getInitParameter("js");//小时
time = Long.parseLong(value)*60*60*1000;
}
response.setDateHeader("Expires", System.currentTimeMillis()+time);
chain.doFilter(request, response);