在Servlet工作完成之前或之后执行doFilter()吗? [英] Is doFilter() executed before or after the Servlet's work is done?

查看:133
本文介绍了在Servlet工作完成之前或之后执行doFilter()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javax.servlet.Filter 对象既可以用于身份验证(Filter需要在任何servlet工作需要完成之前捕获请求),也可以用于XSLT转换(servlet需要完全生成内容)。什么时候实际执行?

The javax.servlet.Filter object can be used both for authentication (where the Filter needs to catch the request before any servlet work needs to be done) and for XSLT translation (where the servlet needs to be completely finished generating content). When does it actually get executed?

我知道这是依赖于实现的(在Web容器上),但这似乎是所有这些都需要解决的问题。

I know this is implementation dependent (on the web container), but this seems to be problem that needs to be solved by all of them.

也许每个过滤器注册的容器都设置了一个配置选项?

Maybe there is a configuration option set somewhere for each Filter registration with the web container?

附加:

此外,什么控制过滤器执行的顺序?为什么FooFilter会在BarFilter之前执行?

Also, what governs the order of Filter execution? Why would FooFilter get executed before BarFilter?

推荐答案

过滤器链本质上包装了servlet调用。链将处理所有链接,直到它到达底部,然后允许servlet运行,然后反向返回链。例如,如果你有一个新的示例过滤器,你的doFilter()方法可能如下所示:

The filter chain in essence wraps the servlet invocation. The chain will process all links until it hits the "bottom", then allow the servlet to run, and then return up the chain in reverse. For example, if you have a new "example filter", your doFilter() method may look like this:

public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
// do pre-servlet work here
chain.doFilter(request, response);
// do post servlet work here

}

这篇关于在Servlet工作完成之前或之后执行doFilter()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆