Java 在 Filter.doFilter 方法中 chain.doFilter 在做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2057607/
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-13 03:05:15  来源:igfitidea点击:

What is chain.doFilter doing in Filter.doFilter method?

javaservlet-filters

提问by giri

In a Filter.doFiltermethod I made this call chain.doFilter.

在一个Filter.doFilter方法中,我打了这个电话chain.doFilter

What is doFilterdoing inside a doFilter? Isn't it a recursive call?

doFilter里面在做什么doFilter?不是递归调用吗?

采纳答案by Bozho

Servlet Filters are an implementation of the Chain of responsibilitydesign pattern.

Servlet 过滤器是责任链设计模式的实现。

All filters are chained (in the order of their definition in web.xml). The chain.doFilter()is proceeding to the next element in the chain. The last element of the chain is the target resource/servlet.

所有过滤器都是链接的(按照它们在 web.xml 中的定义顺序)。的chain.doFilter()是进行到链中的下一个元素。链的最后一个元素是目标资源/servlet。

回答by JaakkoK

It is calling the doFiltermethod of the chainobject, not itself, so no, it won't be recursive.

它正在调用对象的doFilter方法chain,而不是它本身,所以不,它不会是递归的。

The name chainsuggests that you have a sequence of filters, with each filter doing some processing and then passing on to the next in sequence, so each object has a chainmember to point to the next filter in the sequence, which gets called after the filter has performed its own processing. The last in the sequence will then probably have nullas the chainvalue, or it knows on its own that it is the last one in the sequence.

这个名字chain意味着你有一个过滤器序列,每个过滤器做了一些处理,然后传递到序列中的下一个,所以每个对象都有一个chain成员,使其指向序列中的下一个过滤器,它被称为过滤器后,执行了自己的处理。序列中的最后一个可能具有null作为chain值,或者它自己知道它是序列中的最后一个。

回答by axtavt

Internally it invokes doFilterof the next filter in the filter chain, and, when chain is over, it invokes the target servlet.

doFilter在内部调用过滤器链中的下一个过滤器,当链结束时,它调用目标 servlet。

回答by Kallin Nagelberg

By calling chain.doFilter you are handing the request/response to the next filter in your filter chain. If you do not call it then the next filter (probably defined in your web.xml) will not be executed.

通过调用 chain.doFilter,您将请求/响应传递给过滤器链中的下一个过滤器。如果您不调用它,则下一个过滤器(可能在您的 web.xml 中定义)将不会执行。

If you just called doFilter, then yes you would have endless recursion and a stackoverflow. However, you are calling the doFilter method of the filterChain object, which instructs it to execute the next filter.

如果您只是调用 doFilter,那么是的,您将有无限的递归和堆栈溢出。但是,您正在调用 filterChain 对象的 doFilter 方法,该方法指示它执行下一个过滤器。

回答by Paul Wagland

Not having any code that you are talking about, I can only assume that you something like:

没有您正在谈论的任何代码,我只能假设您是这样的:

class Filter implements FilterAPI {
  private FilterAPI chain;
  FilterAPI(FilterAPI chain) { this.chain = chain; }
  @override void doFilter (Set setToFilter) {
    // do some filtering on setToFilter
    chain.doFilter(setToFilter);
  }
}

If that is the case, then you are not calling anything recursively, you are calling doFilter() on a different object. As mentioned on an another answer, this is the well known Chain of Responsibility design pattern.

如果是这种情况,那么您不是在递归调用任何东西,而是在不同的对象上调用 doFilter()。正如另一个答案中提到的,这是众所周知的责任链设计模式。

回答by Vivek Parashar

Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.

导致调用链中的下一个过滤器,或者如果调用过滤器是链中的最后一个过滤器,则导致调用链末尾的资源。