Java过滤器

2018-09-23  本文已影响0人  磊_5d71
image.png

过滤器生命周期

过滤器实现步骤

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

过滤器链

<?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>
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;
        
    }
}
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子元素

上一篇下一篇

猜你喜欢

热点阅读