Is doFilter() executed before or after the Servlet's work is done?

自闭症网瘾萝莉.ら 提交于 2019-12-28 05:14:55

问题


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?

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?

Additional:

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


回答1:


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

}



回答2:


According to the servlet2.3 specification filter is performed according to web.xml configuration of filter- mapping sequence Ref-http://www.programering.com/a/MTMyADOwATI.html



来源:https://stackoverflow.com/questions/1323009/is-dofilter-executed-before-or-after-the-servlets-work-is-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!