You can set an Attribute in your request and check it in your second filter.
public class FirstFilter implements Filter {
//...
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setAttribute("executeSecondFilter", true);
//...
if(someReason)
servletRequest.setAttribute("executeSecondFilter", false);
filterChain.doFilter(servletRequest, servletResponse);
}
}
public class SecondFilter implements Filter {
//..
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (servletRequest.getAttribute("executeSecondFilter") == null || !((Boolean) servletRequest.getAttribute("executeSecondFilter"))) {
filterChain.doFilter(servletRequest, servletResponse);
}
//...
}
}
You can simplify the code above like this :
public class FirstFilter implements Filter {
//...
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//...
if(someReason)
servletRequest.setAttribute("executeSecondFilter", false);
filterChain.doFilter(servletRequest, servletResponse);
}
}
public class SecondFilter implements Filter {
//..
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (servletRequest.getAttribute("executeSecondFilter") != null) {
filterChain.doFilter(servletRequest, servletResponse);
}
//...
}
}
This way you just check the presence of the attribute "executeSecondFilter"