Friday 20 May 2016

what is purpose of calling chain.doFilter() in servlet


what is purpose of calling chain.doFilter() in servlet
chain.doFilter(request, response);

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.

Ref : stackoverflow
author JK

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
...

chain.doFilter(request, response);
}

after executing the method  on calling chain.doFilter() it will call filter present in web.xml in the order of defination.


<filter>
  <filter-name>VoiceLoginFilter</filter-name>
  <filter-class>com.voice.filters.VoiceLoginFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>VoiceLoginFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

No comments:

Post a Comment