过滤器Filter使用

来源:互联网 发布:java 当前时间戳 编辑:程序博客网 时间:2024/06/05 10:18
过滤器Filter也具有生命周期:init()->doFilter()->destroy(),由部署文件中的filter元素驱动。在servlet2.4中,过滤器同样可以用于请求分派器,但须在web.xml中声明,<dispatcher>INCLUDE或FORWARD或REQUEST或ERROR</dispatcher>该元素位于filter-mapping中。

一、批量设置请求编码


Java代码
public class EncodingFilter implements Filter {   
  
    private String encoding = null;   
  
    public void destroy() {   
         encoding = null;   
     }   
  
    public void doFilter(ServletRequest request, ServletResponse response,   
             FilterChain chain) throws IOException, ServletException {   
         String encoding = getEncoding();   
        if (encoding == null){   
             encoding = "gb2312";   
         }   
         request.setCharacterEncoding(encoding);// 在请求里设置上指定的编码   
         chain.doFilter(request, response);   
     }   
  
    public void init(FilterConfig filterConfig) throws ServletException {   
        this.encoding = filterConfig.getInitParameter("encoding");   
     }   
  
    private String getEncoding() {   
        return this.encoding;   
     }   
  
 

Xml代码
<filter>  
    <filter-name>EncodingFilter</filter-name>  
    <filter-class>com.logcd.filter.EncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>gb2312</param-value>  
    </init-param>  
</filter>  
  
<filter-mapping>  
   <filter-name>EncodingFilter</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping>  


二、用filter控制用户访问权限


Java代码
public class SecurityFilter implements Filter {
   public void doFilter(ServletRequest request,
        ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    HttpSession session = req.getSession();
    if (session.getAttribute("username") != null) {//登录后才能访问
        chain.doFilter(request, response);
    } else {
        res.sendRedirect("../failure.jsp");
    }
  }
}


Xml代码
<filter>
    <filter-name>SecurityFilter</filter-name>
    <filter-class>com.logcd.filter.SecurityFilter</filter-class>
</filter>

<filter-mapping>
   <filter-name>SecurityFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

三、过滤链
过滤器A:
<filter>  
    <filter-name>EncodingFilter</filter-name>  
    <filter-class>com.logcd.filter.EncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>gb2312</param-value>  
    </init-param>  
</filter>  
  
<filter-mapping>  
   <filter-name>EncodingFilter</filter-name>  
   <url-pattern>/*</url-pattern>  
</filter-mapping> 

过滤器B:
<filter>
    <filter-name>SecurityFilter</filter-name>
    <filter-class>com.logcd.filter.SecurityFilter</filter-class>
</filter>

<filter-mapping>
   <filter-name>SecurityFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>



两个过滤器,EncodingFilter负责设置编码,SecurityFilter负责控制权限,服务器会按照web.xml中过滤器定义的先后循序组装成一条链,然后一次执行其中的doFilter()方法。执行第一个过滤器的chain.doFilter()之前的代码,第二个过滤器的chain.doFilter()之前的代码,请求的资源,第二个过滤器的chain.doFilter()之后的代码,第一个过滤器的chain.doFilter()之后的代码,最后返回响应。
执行的代码顺序是:


执行EncodingFilter.doFilter()中chain.doFilter()之前的部分:request.setCharacterEncoding("gb2312");
执行SecurityFilter.doFilter()中chain.doFilter()之前的部分:判断用户是否已登录。
如果用户已登录,则访问请求的资源:/admin/index.jsp。
如果用户未登录,则页面重定向到:/failure.jsp。
执行SecurityFilter.doFilter()中chain.doFilter()之后的部分:这里没有代码。
执行EncodingFilter.doFilter()中chain.doFilter()之后的部分:这里也没有代码。

   过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别注意过滤链的执行顺序问题,像EncodingFilter就一定要放在所有Filter之前,这样才能确保在使用请求中的数据前设置正确的编码。
0 0
原创粉丝点击