简体   繁体   中英

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

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

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

Servlet Filters are an implementation of the Chain of responsibility design pattern.

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.

It is calling the doFilter method of the chain object, not itself, so no, it won't be recursive.

The name chain suggests 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 chain member 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 null as the chain value, or it knows on its own that it is the last one in the sequence.

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

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.

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

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.

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.

Yes, it is a recursive call to be precise. If you have a filter set to a folder as such,

/javax.faces.resource/*

then all the resources in this folder will be in this FilterChain. So if you want to do something with the resources you will write

    @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)
      throws IOException, ServletException {
    doSomething(request, response);
    chain.doFilter(request, response);
  }

The call to chain.doFilter(...) will again call doFilter with the next resource in the "chain" of resources and it will be recursive.

The other thing to clarify is that FilterChain does not contain all the Filters you have defined in the web.xml file but is spesific to a given Filter class you have implemented. So you could have a ImagesFilter and a SessionFilter implemantation. For these Filter implementations you will define filter-mapping with URL-pattern. What is contained in the FilterChain is the resources defined in the filter-mapping with URL-pattern, eg

<filter-mapping>
    <filter-name>SessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

So all Resources in the path /* will be in the FilterChain and not the FilterChains SessionFilter and ImagesFilter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM