美文网首页
Java过滤器

Java过滤器

作者: 磊_5d71 | 来源:发表于2018-09-23 05:54 被阅读0次
    image.png

    过滤器生命周期

    • web应用程序启动时,web服务器创建Filter实例对象,以及对象初始化操作
    • 当请求访问与过滤器关联的Web资源时,过滤器拦截请求,完成指定功能。
    • Fliter对象创建后会驻留在内存,在web应用移除或者服务器停止时才销毁
    • 过滤器的创建和销毁由web服务器负责

    过滤器实现步骤

    1、编写java类实现Fliter接口,并实现其doFilter方法
    2、在web.xml文件中对Fliter类进行注册,并设置所拦截的资源。

    过滤器链

    • 在一个web应用中,多个过滤器组合起来称之为一个过滤器链
    • 过滤器的调用顺序取决于过滤器在web.xml文件中的注册顺序
    • 案例1 中文乱码过滤器实现
    • web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns: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>FilterDemoWebProj</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <!-- 字符乱码过滤器配置 -->
      <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.alan.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>systemName</param-name>
          <param-value>filter Encoding</param-value>
        </init-param>
        <init-param>
          <param-name>version</param-name>
          <param-value>1.0</param-value>
        </init-param>
        <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>com.alan.filter.SessionFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/message.jsp</url-pattern>
      </filter-mapping>
    </web-app>
    
    • filter 中文乱码
    package com.alan.filter;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class CharacterEncodingFilter implements Filter {
    
        //定义FilterConfig对象,将init传入的config对其赋值
        private FilterConfig config;
        
        
        public CharacterEncodingFilter() {
            // TODO Auto-generated constructor stub
        }
    
        public void destroy() {     
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
    
    //      String systemName = config.getInitParameter("systemName");
    //      String version = config.getInitParameter("version");
    //      System.out.println(systemName);
    //      System.out.println(version);
            
            //中文编码过滤器实现
            String charSet = config.getInitParameter("charSet");
            request.setCharacterEncoding(charSet);
    
                //此方法通知web服务器,连接请求已经执行完成
                chain.doFilter(request, response);
    
        }
    
        public void init(FilterConfig fConfig) throws ServletException {
            this.config = fConfig;
            
        }
    }
    
    • filter 安全控制
    package com.alan.filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class SessionFilter implements Filter {
    
        public SessionFilter() {
    
        }
    
        public void destroy() {
    
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
    
            // 用户登陆安全控制
            // 1、向下转型
            HttpServletRequest hresquest = (HttpServletRequest) request;
            HttpServletResponse hresponse = (HttpServletResponse) response;
            // 2、判断全局域中的username是否有值
            String username = (String) hresquest.getServletContext().getAttribute("username");
            if (username == null) {
                hresponse.sendRedirect(hresquest.getContextPath() + "/index.jsp?flag=1");
                return;
    
            } else { // 此方法通知web服务器,连接请求已经执行完成
                chain.doFilter(request, response);
                return;
            }
        }
    
        public void init(FilterConfig fConfig) throws ServletException {
    
        }
    
    }
    

    dispatcher子元素

    • <dispatcher></dispatcher>没有配置,默认为request
    • 包括 request include forward error 四种配置方式
    • 如果是重定向的话 需要配置为request???
    • 可以对一个过滤器配置多个dispatcher子元素

    相关文章

      网友评论

          本文标题:Java过滤器

          本文链接:https://www.haomeiwen.com/subject/yafxoftx.html