Spring In Action

过滤器的实现

2017-02-14  本文已影响153人  CodingHou

Filter过滤器API

Servlet过滤器API包含了3个接口,它们都在javax.servlet包中,分别是Filter接口、FilterChain接口和FilterConfig接口。
Filter接口(源码)

public interface Filter {

        public void init(FilterConfig filterConfig) throws ServletException;
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
    
        public void destroy();
}

所有的过滤器都要实现Filter接口,该接口定义了init(),doFilter(),destory()三个方法。

1.init(FilterConfig filterConfig)

2.doFilter(ServletRequest request,ServletResponse response,FilterChain chain)

3. public void destory()

FilterChain接口

public interface FilterChain {
    public void doFilter(ServletRequest request, ServletResponse response)      
                throws IOException, ServletException;
}

FilterCOnfig接口

public interface FilterConfig {

  //返回web.xml部署文件中定义的该过滤器的名称
    public String getFilterName();
    
  //返回调用者所处的servlet上下文
    public ServletContext getServletContext();
    
  //返回过滤器初始化参数值的字符串形式,当参数不存在时,返回nul1.name是初始化参数名
    public String getInitParameter(String name);
    
  //以Enumeration形式返回过滤器所有初始化参数值,如果没有初始化参数,返回为空
    public Enumeration getInitParameterNames();
}
上一篇 下一篇

猜你喜欢

热点阅读