Filter
来源:互联网 发布:淘宝卖家强制取消订单 编辑:程序博客网 时间:2024/06/15 19:34
过滤器 (Filter)1、概述:过滤器概述过滤器就像一个保安,可以对请求和响应进行拦截。2、编写过滤的步骤:1)编写一个类,实现javax.servlet.Filter接口,这样的类一般称之为过滤器类public class FilterDemo1 implements Filter {public void init(FilterConfig filterConfig) throws ServletException {System.out.println("FilerDemo1初始化了");}//对于每一次的用户访问,都会调用该方法。public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println("FilterDemo1拦截前");chain.doFilter(request, response);//放行,让目标资源执行System.out.println("FilterDemo1拦截后");}public void destroy() {// TODO Auto-generated method stub}}2)在web.xml中进行配置,要拦截哪些资源。<filter><filter-name>FilterDemo1</filter-name><filter-class>cn.jxn.filter.FilterDemo1</filter-class></filter><filter-mapping><filter-name>FilterDemo1</filter-name><url-pattern>/*</url-pattern></filter-mapping>3、过滤器的执行过程:1)过滤器只会被初始化一次,应用被加载时就完成了初始化。2)多个过滤器的拦截顺序是按照web.xml中filter-mapping元素的出现顺序进行拦截的。4、过滤器简单案例:1)界面输出中文及中文请求参数(POST方式有效)编码过滤器:SetCharacterEncodingFilter.javaweb.xml中的配置:<filter><filter-name>SetCharacterEncodingFilter</filter-name><filter-class>cn.jxn.filter.example.SetCharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param> </filter> <filter-mapping><filter-name>SetCharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern> </filter-mapping>//界面输出中文及中文请求参数(POST方式有效)编码过滤器public class SetCharacterEncodingFilter implements Filter {private FilterConfig filterConfig;public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {String encodingValue = filterConfig.getInitParameter("encoding");//获取过滤器配置中的参数if(encodingValue==null)//如果没有配置该参数encodingValue="UTF-8";//给定默认值UTF-8request.setCharacterEncoding(encodingValue);//只对POST请求方式有用response.setContentType("text/html;charset="+encodingValue);chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}}2)控制动态资源(Servlet JSP)不要缓存的过滤器web.xml中的配置<filter><filter-name>NoCacheFilter</filter-name><filter-class>cn.jxn.filter.example.NoCacheFilter</filter-class></filter><filter-mapping><filter-name>NoCacheFilter</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping><filter-mapping><filter-name>NoCacheFilter</filter-name><url-pattern>/servlet/*</url-pattern></filter-mapping>// 控制动态资源(Servlet JSP)不要缓存的过滤器public class NoCacheFilter implements Filter {public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest)req;HttpServletResponse response = (HttpServletResponse)resp;response.setDateHeader("Expires", 0);response.setHeader("Cache-Control", "no-cache");response.setHeader("Pragma", "no-cache");chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {}}3)控制html、css、js等静态资源的缓存时间的过滤器web.xml中的配置<filter><filter-name>NeedCacheFilter</filter-name><filter-class>cn.jxn.filter.example.NeedCacheFilter</filter-class><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></filter><filter-mapping><filter-name>NeedCacheFilter</filter-name><url-pattern>*.html</url-pattern></filter-mapping><filter-mapping><filter-name>NeedCacheFilter</filter-name><url-pattern>*.css</url-pattern></filter-mapping><filter-mapping><filter-name>NeedCacheFilter</filter-name><url-pattern>*.js</url-pattern></filter-mapping>public class NeedCacheFilter implements Filter {private FilterConfig filterConfig;public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {System.out.println("控制缓存时间的过滤器过滤了");HttpServletRequest request = (HttpServletRequest)req;HttpServletResponse response = (HttpServletResponse)resp;//获取html、css、js各自的缓存时间,如果没有,给个默认值是1小时int time = 1;String uri = request.getRequestURI();// /day19/1.htmlString extendName = uri.substring(uri.lastIndexOf(".")+1);if("html".equals(extendName)){//访问的html资源String value = filterConfig.getInitParameter("html");if(value!=null){time = Integer.parseInt(value);}}if("css".equals(extendName)){//访问的css资源String value = filterConfig.getInitParameter("css");if(value!=null){time = Integer.parseInt(value);}}if("js".equals(extendName)){//访问的js资源String value = filterConfig.getInitParameter("js");if(value!=null){time = Integer.parseInt(value);}}response.setDateHeader("Expires", System.currentTimeMillis()+time*60*60*1000);chain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}}
0 0
- Filter
- Filter
- Filter
- filter
- filter
- Filter
- filter
- Filter
- filter
- filter
- Filter
- Filter
- Filter
- Filter
- filter
- Filter
- Filter
- filter
- 第三周——基于对象的程序设计——成绩类多文件组织
- World Wind Java开发之十五——加载三维模型
- Java开发中的23种设计模式详解
- Sicily 1515. 魔版C
- 学习OpenCV读书笔记——第二章
- Filter
- 第二周 项目三 时间类(1)
- python 字典(dict)按键和值排序
- 多益网络2015校园招聘第二次笔试题
- 欧拉函数以部分性质
- 实时更新widget
- redmine 导出中文乱码
- 编译原理之DFA自动机的
- 使用Xcode自带的单元测试