java javax.servlet.FilterChain 的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14693553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:04:35  来源:igfitidea点击:

What is the purpose of javax.servlet.FilterChain?

javaservletsservlet-filters

提问by barclay

I inherited a Struts 1 app that heavily utilizes FilterChain and I don't understand the benefit of this extremely obfuscating code.

我继承了一个大量使用 FilterChain 的 Struts 1 应用程序,但我不明白这种极度混淆的代码的好处。

"In the Servlet API, you normally use a Servlet when you want to control, preprocess and/or postprocess specific requests. But when you want to filter/modify common requests and/or responses based on specific conditions, then a Filter is much more suitable."

“在 Servlet API 中,当您想要控制、预处理和/或后处理特定请求时,您通常会使用 Servlet。但是当您想要根据特定条件过滤/修改常见请求和/或响应时,那么过滤器就更多了合适的。”

Every request in my app is based on specific conditions, e.g., a merchant id or a search term. But it seems like placing a request inside a whole chain of stuff that completely hides what is going on from the developer trying to trace the cause of an error, is nuts.

我的应用程序中的每个请求都基于特定条件,例如,商家 ID 或搜索词。但是,似乎将请求放入一整个链中完全隐藏了开发人员试图追踪错误原因所发生的事情,这很疯狂。

回答by BalusC

The FilterChain#doFilter()call just continuesthe HTTP request to the destination, following exactly the same path as if you didn't use a filter in first place. This is usually a servlet class or even a JSP file. So, in order to debug problematic code, better put a breakpoint in the target code, not in the filter if it doesn't contain any code of interest.

FilterChain#doFilter()调用只是将 HTTP 请求继续发送到目标,遵循完全相同的路径,就好像您没有首先使用过滤器一样。这通常是一个 servlet 类,甚至是一个 JSP 文件。因此,为了调试有问题的代码,最好在目标代码中放置一个断点,如果它不包含任何感兴趣的代码,则不要在过滤器中放置。

回答by barclay

My coworker (who's not registered on SO) explained that it's for applying global functionality to an app that you don't want to do in every single controller, such as checking if the user is logged in.

我的同事(未在 SO 上注册)解释说,这是为了将全局功能应用于您不想在每个控制器中都执行的应用程序,例如检查用户是否已登录。