web.xml中过滤器实现总结

在web开发过程中,往往需要在web.xml中进行过滤配置,对用户的请求以及服务器的响应进行过滤。 

 过滤器的实现总结:

  1、定义一个类实现Filter接口
  2、在init()初始化方法中获取web.xml中配置的参数值

  3、在doFilter方法中实现过滤逻辑

Filter接口提供了三个方法:

init():初始化,读取web.xml中配置的参数

doFilter():实现过滤逻辑

destory():注销该过滤器

public interface Filter {
    default void init(FilterConfig filterConfig) throws ServletException {
    }

    void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException;

    default void destroy() {
    }
}

下面举例说明过滤器的用法:该过滤器禁止某些IP访问应用

定义类实现Filter接口:

package Filter;

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 java.io.IOException;
import java.io.PrintWriter;

public class IpFilter implements Filter {
    protected FilterConfig filterConfig ;
    protected String ip;
    //过滤器初始化的时候调用,在过滤器初始化过程中从web配置文件读取参数的内容
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.ip = this.filterConfig.getInitParameter("ip");
    }
    //实现过滤操作
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String remoteIp = servletRequest.getRemoteAddr();
        if (remoteIp.equals(ip)) {
            servletResponse.setCharacterEncoding("GBK");
            PrintWriter out = servletResponse.getWriter();
            out.println("你的IP地址被禁止访问.");
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}

web.xml中过滤器的配置:

xml version="1.0" encoding="UTF-8"?>
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
    
        IpFilter
        Filter.IpFilter
        
        
            ip
            127.0.0.1
        
    
    
        IpFilter
        /*
    
    
    
        SetCharacterEncoding
        
            Filter.SetCharacterEncodingFilter
        
        
            encoding
            gb2312
        
        
            enable
            true
        
    
    
        SetCharacterEncoding
        /*
    
    
    
        HelloWorlds
        HelloWorld.HelloServlet
    
    
        HelloWorlds
        /HelloWorld
    
    
        AcceptForm
        servlets.AcceptForm
    
    
        AcceptForm
        /AcceptForm
    
    
        AcceptLink
        servlets.AcceptLink
    
    
        AcceptLink
        /AcceptLink
    
    
        FileReader
        servlets.FileReader
    
    
        FileReader
        /FileReader
    
    
        FileWriter
        servlets.FileWriter
    
    
        FileWriter
        /FileWriter
    



你可能感兴趣的:(WEB)