码迷,mamicode.com
首页 > 其他好文 > 详细

Tomcat过滤器模拟

时间:2014-09-29 20:44:01      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   java   sp   div   c   

直接上代码Filter.java

public interface Filter {
    //过滤器
    public void doFilter(Request request,Response response,FilterChain chain);

}

FilterChain.java

public class FilterChain {
    //存放所有的过滤器
    private List<Filter> filters ;
    //记录当前过滤索引
    private int index;
    //过滤链初始化
    public FilterChain()
    {
        filters = new ArrayList<Filter>(10);
    }
    
    public void addFilter(Filter filter)
    {
        filters.add(filter);
    }
    /**
     * 迭代过滤
     */
    public void doFilter(Request request,Response response)
    {
        if ( index >= filters.size() )
            return;
        filters.get( index++ ).doFilter(request, response, this);
    }

}

Request.java

public class Request {
    
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    @Override
    public String toString() {
        return this.value;
    }

}

Response.java

public class Response {
    
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    @Override
    public String toString() {
        return this.value;
    }

}

FilterOne.java

public class FilterOne implements Filter{

    /**
     * 过滤FUCK字符
     */
    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        System.out.println("进入过滤器FilterOne---------------------------------------");
        //执行下一个过滤器
        chain.doFilter(request, response);
        
        System.out.println("出去过滤器FilterOne---------------------------------------");
    }

}

FilterTwo.java

public class FilterTwo implements Filter{

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        System.out.println("进入过滤器FilterTwo---------------------------------------");
        //执行下一个过滤器
        chain.doFilter(request, response);
        
        System.out.println("出去过滤器FilterTwo---------------------------------------");
    }
    
}

Test.java

public class Test {
    
    public static void main(String[] args) {
        FilterChain chain = new FilterChain();
        chain.addFilter(new FilterOne());
        chain.addFilter(new FilterTwo());
        chain.doFilter(new Request(), new Response() );
    }

}

运行结果

进入过滤器FilterOne---------------------------------------
进入过滤器FilterTwo---------------------------------------
出去过滤器FilterTwo---------------------------------------
出去过滤器FilterOne---------------------------------------

 

Tomcat过滤器模拟

标签:style   blog   color   os   ar   java   sp   div   c   

原文地址:http://www.cnblogs.com/stay-9527/p/4000869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!