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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:51:15  来源:igfitidea点击:

What is the use of filter and chain in servlet?

javaservletsservlet-filterschain

提问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.

过滤器是用来补充 Servlet 的。对于用法,你应该阅读这个,过滤器的精华。过滤器是使用责任链GoF 模式实现的。

回答by Prateek Joshi

What are Filters ?

什么是过滤器?

Filtersare used to intercept and process requestsbefore they are sent to servlets(in case of request) .

Filters用于intercept and process requests前它们被发送到小服务程序(在请求的情况)。

OR

或者

Filtersare used to intercept and process a responsebefore they are sent back to client by a servlet.

Filtersintercept and process a response在它们被 servlet 发送回客户端之前已经习惯了。

enter image description here

在此处输入图片说明

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 called every timethe 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 dois 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 the order of the invocation of filtersand drivenby the filter elementsyou defined in the DD.

你的filters do not know anything about the other filters and servletFilterChain知道order of the invocation of filtersdrivenfilter elements您在DD.