package com.example.cor.chain; import com.example.cor.filter.Filter; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class ApplicationFilterChain implements FilterChain{ private static final int INCREMENT = 10; // target private Servlet servlet; public void setServlet(Servlet servlet) { this.servlet = servlet; } // manage filters private Filter[] filters = new Filter[0]; // filter count private int n; public void addFilter(Filter filter) { for (Filter f : filters) { if (f == filter) { return; } } // no capacity, if (n == filters.length) { Filter[] newFilters = new Filter[n + INCREMENT]; System.arraycopy(this.filters,0,newFilters,0,n); filters = newFilters; } filters[n++] = filter; } // call next filter private int pos;// current filter index @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { if (pos < n) { // call next filter Filter filter = filters[pos++]; filter.doFilter(request,response,this); return; } // call target servlet.service(request,response); } }
package com.example.cor.chain; import com.example.cor.filter.Filter; import javax.servlet.Servlet; public class ApplicationFilterFactory { public static FilterChain createFilterChain(Servlet servlet, Filter ... filters) { if (servlet==null) { return null; } ApplicationFilterChain chain = new ApplicationFilterChain(); chain.setServlet(servlet); // add filters for (Filter filter : filters) { chain.addFilter(filter); } return chain; } }
package com.example.cor.chain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public interface FilterChain { void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException; }
package com.example.cor.filter; import com.example.cor.chain.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public interface Filter { void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException; }
package com.example.cor.filters; import com.example.cor.chain.FilterChain; import com.example.cor.filter.Filter; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class CostFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("CostFilter pre"); chain.doFilter(request,response); System.out.println("CostFilter post"); } }
标签:Filter,filter,责任,filters,import,设计模式,servlet,javax From: https://www.cnblogs.com/wscp/p/18462650