1.简介
3.应用场景
<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID"version="3.1"><display-name>FilterDemo</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!--字符集编码过滤器配置--><filter><filter-name>characterEncodingFilter</filter-name><filter-class>filter.CharacterEncodingFilter</filter-class><init-param><param-name>charset</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 用户登录安全控制过滤器配置 --><filter><filter-name>sessionFilter</filter-name><filter-class>filter.SessionFilter</filter-class></filter><filter-mapping><filter-name>sessionFilter</filter-name><url-pattern>/message.jsp</url-pattern></filter-mapping></web-app>
◆ Web资源权限访问控制(如:登录系统)
packagefilter; importjava.io.IOException; importjavax.servlet.Filter; importjavax.servlet.FilterChain; importjavax.servlet.FilterConfig; importjavax.servlet.ServletException; importjavax.servlet.ServletRequest; importjavax.servlet.ServletResponse; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /*** 用户登录安全控制过滤器*/publicclassSessionFilterimplementsFilter { @Overridepublicvoiddestroy() { } @OverridepublicvoiddoFilter(ServletRequestrequest, ServletResponseresponse, FilterChainchain) throwsIOException, ServletException { HttpServletRequesthrequest= (HttpServletRequest) request;// 涉及到HTTP请求处理,转型处理HttpServletResponsehresponse= (HttpServletResponse) response;// 涉及到HTTP请求处理,转型处理StringloginUser= (String) hrequest.getSession().getAttribute("loginUser");// 判断用户是否完成了登录操作,session中是否存储用户名if (loginUser==null) { hresponse.sendRedirect(hrequest.getContextPath() +"/index.jsp?flag=1");// 未登录,系统强制重定向至登录页面return; } else { chain.doFilter(hrequest, hresponse); return; } } @Overridepublicvoidinit(FilterConfigconfig) throwsServletException { } }
◆ 请求字符集编码处理
packagefilter; /*** 字符集编码过滤器*/importjava.io.IOException; importjavax.servlet.Filter; importjavax.servlet.FilterChain; importjavax.servlet.FilterConfig; importjavax.servlet.ServletException; importjavax.servlet.ServletRequest; importjavax.servlet.ServletResponse; publicclassCharacterEncodingFilterimplementsFilter { privateFilterConfigconfig; @Overridepublicvoiddestroy() { } @OverridepublicvoiddoFilter(ServletRequestrequest, ServletResponseresponse, FilterChainchain) throwsIOException, ServletException { request.setCharacterEncoding(config.getInitParameter("charset")); // 根据过滤器配置字符集,设置请求字符集编码// System.out.println("characterEncodingFilter 请求预处理"); // 测试过滤器(链)工作流程使用chain.doFilter(request, response); // 通知web服务器已经完成,或请求下一个过滤器// System.out.println("characterEncodingFilter 响应后处理"); // 测试过滤器(链)工作流程使用 } @Overridepublicvoidinit(FilterConfigconfig) throwsServletException { this.config=config; } publicFilterConfiggetConfig() { returnconfig; } publicvoidsetConfig(FilterConfigconfig) { this.config=config; } }
◆ 内容敏感字符词汇过滤
◆ 响应信息压缩
◆ 404 错误页面
◆ 在过滤器中执行触发新的过滤器◆ 重定向(REQUEST)◆ 多个 dispatcher 子元素的配置
3.过滤器 web.xml 配置步骤(类似Servlet的web.xml配置)
1、注册过滤器
2、拦截映射配置
4.过滤器配置
5.过滤器——转发与重定向(死循环)
@OverridepublicvoiddoFilter(ServletRequestservletRequest, ServletResponseservletResponse, FilterChainfilterChain) throwsIOException, ServletException { System.out.println("Filter 开始》》》》》》》》》》》》》》"); /*filterChain.doFilter(servletRequest, servletResponse);*/System.out.println("首次监听启动"); servletRequest.getRequestDispatcher("main.jsp").forward(servletRequest, servletResponse); // 重定向省略...System.out.println("Filter 结束》》》》》》》》》》》》》》"); }
<filter><filter-name>SimpleFilter</filter-name><filter-class>SimpleFilter</filter-class></filter><filter-mapping><filter-name>SimpleFilter</filter-name><url-pattern>/index.jsp</url-pattern></filter-mapping><filter-mapping><filter-name>SimpleFilter</filter-name><url-pattern>/main.jsp</url-pattern></filter-mapping>
1)对于请求转发;forward跳转到main.jsp url不变,因此过滤器只过滤一次/index.jsp的访问;当然这是针对dispatcher类型是默认的REQUEST而言,如果将dispatcher类型改为FORWARD,那么即使url不变,也可以过滤Forward跳转行为,然后就会死循环访问main.jsp。
2)对于请求重定向,url一直变,然后一直过滤,一直死循环。
6.多个过滤器执行顺序
<!--过滤顺序:谁的写在上面,谁先被过滤--><filter><filter-name>FilterTest01</filter-name><filter-class>com.Higgin.web.filter.FilterTest01</filter-class></filter><filter-mapping><filter-name>FilterTest01</filter-name><url-pattern>/*</url-pattern> <!-- 过滤所有 --></filter-mapping><filter><filter-name>FilterTest02</filter-name><filter-class>com.Higgin.web.filter.FilterTest02</filter-class></filter><filter-mapping><filter-name>FilterTest02</filter-name><url-pattern>/*</url-pattern> <!-- 过滤所有 --></filter-mapping>
7.待更新...