[英]What is the use of filter and chain in servlet?
chain.doFilter(req,res);
We used this in a servlet program. 我们在servlet程序中使用了它。 I want to know what is the use of the method
doFilter()
in a servlet? 我想知道servlet中
doFilter()
方法的用法是什么? Also what is the use of filter and chain concept in Java servlets? 还有什么在Java servlet中使用过滤器和链概念?
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. 关键是每个过滤器都保持在它所映射到的每个servlet的“前面”和“后面”。 So if you have a filter around a servlet, you'll have:
因此,如果您在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 not to call chain.doFilter(..)
in which case the servlet will never be called. 您还可以选择不调用
chain.doFilter(..)
在这种情况下,永远不会调用servlet。 This is useful for security purposes - for example you can check whether there's a user logged-in. 这对于安全目的很有用 - 例如,您可以检查是否有用户登录。
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
发送到servlet之前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发送回客户端之前intercept and process a response
。
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.every time
容器确定应将过滤器应用于页面every time
,都会调用doFilter()
。
It takes three arguments
它需要
three arguments
-> ServletRequest - > ServletRequest
-> ServlerResponse - > ServlerResponse
-> FilterChain - > FilterChain
All the functionality that your filter supposed to do
is implemented inside doFilter()
method. functionality that your filter supposed to do
执行的所有functionality that your filter supposed to do
都是在doFilter()
方法中实现的。
What is FilterChain ? 什么是FilterChain?
Your
filters do not know anything about the other filters and servlet
.您的
filters do not know anything about the other filters and servlet
。 FilterChain knows theorder of the invocation of filters
anddriven
by thefilter elements
you defined in theDD
.FilterChain知道
order of the invocation of filters
的order of the invocation of filters
并由您在DD
定义的filter elements
driven
。
Filters are there to complement Servlets. 过滤器可以补充Servlets。 For the usage, you should read this, The Essentials of Filters .
对于用法,您应该阅读“过滤器的基本要素” 。 Filters are implemented using Chain of Responsibility GoF pattern.
过滤器使用Chain of Responsibility GoF模式实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.