Java servlet中filter和chain有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4122870/
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
What is the use of filter and chain in servlet?
提问by Vinoth Kumar
chain.doFilter(req,res);
We used this in a servlet program. I want to know what is the use of the method doFilter()
in a servlet?
Also what is the use of filter and chain concept in Java servlets?
chain.doFilter(req,res);
我们在 servlet 程序中使用了它。我想知道doFilter()
servlet中的方法有什么用?另外,Java servlet 中过滤器和链概念的用途是什么?
回答by Bozho
Servlet filters are implementation of the chain of responsibility pattern
Servlet 过滤器是责任链模式的实现
The point is that each filter stays "in front" and "behind" each servlet it is mapped to. So if you have a filter around a servlet, you'll have:
关键是每个过滤器都位于它所映射到的每个 servlet 的“前面”和“后面”。因此,如果您有一个围绕 servlet 的过滤器,您将拥有:
void doFilter(..) {
// do stuff before servlet gets called
// invoke the servlet, or any other filters mapped to the target servlet
chain.doFilter(..);
// do stuff after the servlet finishes
}
You also have the option notto call chain.doFilter(..)
in which case the servlet will never be called. This is useful for security purposes - for example you can check whether there's a user logged-in.
您还可以选择不调用,chain.doFilter(..)
在这种情况下将永远不会调用 servlet。这对于安全目的很有用 - 例如,您可以检查是否有用户登录。
回答by Adeel Ansari
Filters are there to complement Servlets. For the usage, you should read this, The Essentials of Filters. Filters are implemented using Chain of ResponsibilityGoF pattern.
回答by Prateek Joshi
What are Filters ?
什么是过滤器?
Filters
are used to intercept and process requests
before they are sent to servlets(in case of request) .
Filters
用于intercept and process requests
前它们被发送到小服务程序(在请求的情况)。
OR
或者
Filters
are used to intercept and process a response
before they are sent back to client by a servlet.
Filters
intercept and process a response
在它们被 servlet 发送回客户端之前已经习惯了。
Why they are used ?
为什么使用它们?
-Filters can perform security checks.
-过滤器可以执行安全检查。
-Compress the response stream.
- 压缩响应流。
-Create a different response.
- 创建不同的响应。
What does doFilter() do ?
doFilter() 有什么作用?
The
doFilter()
is calledevery time
the container determines that the filter should be applied to a page.
的
doFilter()
被称为every time
容器确定该过滤器应被应用到页面。
It takes three arguments
它需要 three arguments
->ServletRequest
-> ServletRequest
->ServlerResponse
->服务器响应
->FilterChain
->过滤链
All the functionality that your filter supposed to do
is implemented inside doFilter()
method.
所有的functionality that your filter supposed to do
都在doFilter()
方法内部实现。
What is FilterChain ?
什么是过滤链?
Your
filters do not know anything about the other filters and servlet
. FilterChainknows theorder of the invocation of filters
anddriven
by thefilter elements
you defined in theDD
.
你的
filters do not know anything about the other filters and servlet
。 FilterChain知道order of the invocation of filters
和driven
由filter elements
您在DD
.