您可以在请求中设置一个属性,然后在第二个过滤器中对其进行检查。
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); } //... }}
您可以像这样简化上面的代码:
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); } //... }}
这样,您只需检查属性“ executeSecondFilter”的存在
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)