当把请求提交时经过过滤器过滤,如下所示:
代码1;
filterchain.dofilter();
代码2;
请问对客户端请求过滤时代码2执行吗?代码2是不是对服务器回应信息的过滤?

解决方案 »

  1.   

    在servlet2.3规范中filter有类型区分。看样子LZ想实现的是一个request类型的。servlet执行的模型类型于一条链,而filter则是在这条链中插入“一脚”,当来时和回来时截获住。所以你上面的理解是对的。但是filter可以在这条链的什么地方插入“一脚”呢,这个就是和类型相关了,如request就是客户端与服务器之间接墒的地方,而FORWARD则是在你代码中调用forward到另一个组件时触发,还有INCLUDE,ERROR也类似
    The dispatcher has four legal values: FORWARD, REQUEST, INCLUDE,
    and ERROR. A value of FORWARD means the Filter will be applied
    under RequestDispatcher.forward() calls.  A value of REQUEST
    means the Filter will be applied under ordinary client calls to
    the path or servlet. A value of INCLUDE means the Filter will be
    applied under RequestDispatcher.include() calls.  A value of
    ERROR means the Filter will be applied under the error page
    mechanism.  The absence of any dispatcher elements in a
    filter-mapping indicates a default of applying filters only under
    ordinary client calls to the path or servlet.
      

  2.   

    差不多,filterchain.dofilter()会继续会执行链后面的主件,如servlet,jsp等等,如果没有这个,就直接返回而不会继续执行了,所以当filterchain.dofilter()返回时链后面的主件都执行完了
      

  3.   

    再麻烦楼上一下:
    过滤request用代码1,filterchain.dofilter()调用过滤器链中中下一个过滤器继续过虑,当都过滤后,把请求提交给目标资源,目标资源处理后,再把处理结果response用代码2过滤,最后返回客户端。是这样吗?
      

  4.   

    你问这么多,加上断点调试一下不就知道了吗?过滤器就是要在访问所请求的网页前做一些事情,访问完后再做一些事情。除非你在要访问的页面中跳转了。System.out.println("请求之前");
    filterchain.dofilter();
    System.out.println("请求之后");有点装饰模式的意思