Filter(五)FilterChain执行顺序

当使用多个Filter时,这些Filter将形成一个Filter链,web服务器在实例化Filter对象时,会将此Filter链传递给doFilter的FilterChain参数。当一个Filter执行doFilter方法时,在此方法中执行FilterChain参数的doFilter方法,将会执行下一个Filter(Filter链下一个还是Filter时)或者访问WEB资源(Filter链中没有Filter了),如图所示:

Filter(五)FilterChain执行顺序_第1张图片

下面是在web.xml中配置多个Filter,通过程序来演示各个Filter的其执行先后顺序。

	
  	FilterUtils
  	cn.itcast.utils.FilterUtils
  	
  
  
  	FilterUtils
  	/*
  	REQUEST
  
  
  	
  	EncodeFilter
  	cn.itcast.utils.EncodeFilter
  	
  		encoding
  		UTF-8
  	
  	
  
  
  	EncodeFilter
  	/*
  	REQUEST
  
  
  	
  	FilterJsp
  	cn.itcast.utils.FilterJsp
  	
  
  
  	FilterJsp
  	/*
  	REQUEST
  

三个Filter

public class FilterUtils implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("FilterUtils destory....");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		System.out.println("FilterUtils start....");
		chain.doFilter(req, resp);
		System.out.println("FilterUtils end....");
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		System.out.println("FilterUtils init....");
	}

}

public class EncodeFilter implements Filter {
	private String encode;
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("EncodeFilter destroy....");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
//		req.setCharacterEncoding(encode);
//		resp.setCharacterEncoding(encode);
//		resp.setContentType("text/html;charset="+encode+"");
//		chain.doFilter(req, resp);
		
		System.out.println("EncodeFilter start....");
		chain.doFilter(req, resp);
		System.out.println("EncodeFilter end....");
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
	//	encode = filterConfig.getInitParameter("encoding");
		System.out.println("EncodeFilter init....");
	}

}

public class FilterJsp implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("FilterJSP destory....");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
//		System.out.println("do Filter....");
//		req.setCharacterEncoding("UTF-8");
//		resp.setCharacterEncoding("UTF-8");
//		resp.setContentType("text/html;charset=UTF-8");
//		chain.doFilter(req, resp);
		
		System.out.println("FilterJsp start....");
		chain.doFilter(req, resp);
		System.out.println("FilterJsp end....");
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
//		System.out.println("init....");
//		System.out.println("Filter Name is:"+config.getFilterName());
//		Enumeration enumation = config.getInitParameterNames();
//		String name;
//		while(enumation.hasMoreElements()){
//			name = (String) enumation.nextElement();
//			System.out.println("name is:"+name);
//			System.out.println("value is:"+config.getInitParameter(name));
//		}
		
		System.out.println("FilterJsp init....");
	}

}

//初始化结果:

FilterUtils init....
EncodeFilter init....
FilterJsp init....
//访问WEB资源时的结果

FilterUtils start....
EncodeFilter start....
FilterJsp start....
你好中国
FilterJsp end....
EncodeFilter end....
FilterUtils end....

//web服务器关闭时结果:

FilterUtils destory....
EncodeFilter destroy....
FilterJSP destory....

你可能感兴趣的:(javaweb)