在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.
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.
过滤request用代码1,filterchain.dofilter()调用过滤器链中中下一个过滤器继续过虑,当都过滤后,把请求提交给目标资源,目标资源处理后,再把处理结果response用代码2过滤,最后返回客户端。是这样吗?
filterchain.dofilter();
System.out.println("请求之后");有点装饰模式的意思