使用filter导致服务器返回的页面始终是空白---在doFilter中漏写了chain.doFilter()

本文探讨了在部署特定过滤器后导致网站所有页面显示为空白的问题。通过分析SimpleFilter的实现,指出了未调用filterChain.doFilter()导致的问题,并提供了正确的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天调代码的时候,突然发现,服务器开着,什么都没有问题,当我把下面这个filter给deploy了以后,访问所有的页面就都是空白。

后来发现,是因为在代码路径中,有一条路径没有调用filterChain.doFilter(request, response),想来也是,你这不就等于把HttpServletRequest给丢失了么。

web.xml中,在enable=”false”的情况下,会出现我上面描述的问题,代码如下

package org.foo.filterdemo ;

import java.io.* ;

import javax.servlet.* ;

public class SimpleFilter implements Filter {

    private boolean enable = false;

    public void init(FilterConfig config)
          throws ServletException{
        String enableString = config.getInitParameter("enable");
        if (enableString != null && enableString.equalsIgnoreCase("true")) {
            this.enable = true;
        }
        String initParam = config.getInitParameter("ref") ;
        try {
            initParam = new String(initParam.getBytes("iso-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(this + ": init(), init-param = " + initParam);
    }
    public void doFilter(ServletRequest request,
              ServletResponse response,
              FilterChain chain)
              throws IOException,
                     ServletException{
        if (this.enable) {
            System.out.println(this + ": doFilter()") ;
            chain.doFilter(request, response);
        } else {
            // chain.doFilter(request, response); // 如果你发现页面始终是空白,问题就出在这里,不能缺少这行代码
        }
    }
    public void destroy(){
        // clean up
        System.out.println(this + ": destroy()");
    }
}

web.xml

  注意enable选项为false的时候,如果上面被注释掉的代码没取消注释,那么就会出问题,因为这里的url-pattern设置了此filter对站点的所有页面都要起作用

<filter> 
        <filter-name>simple-filter</filter-name> 
        <filter-class>org.foo.filterdemo.SimpleFilter</filter-class> 
        <init-param> 
            <param-name>enable</param-name> 
            <param-value>true</param-value> 
        </init-param>
        <init-param> 
            <param-name>ref</param-name> 
            <param-value>这是一串中文,给SimpleFilter的</param-value> 
        </init-param>  
    </filter>
    <filter-mapping>
        <filter-name>simple-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

实际上,由于chain.doFilter()是不可缺少的,因此添加了enable参数的filter的代码应该这样写:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException{
        if (this.enable) {
            // do some work
        }
        chain.doFilter(request, response);
}

Article from “rldts” in “http://www.cnblogs.com/
Original Article : http://www.cnblogs.com/qrlozte/p/3178454.html

Copyright rldts
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值