如果Servlet無法使用了,調用包裝器的 unload 方法
// Allocate a servlet instance to process this request try { if (!unavailable) { servlet = wrapper.allocate(); } } ... // Acknowlege the request try { response.sendAcknowledgement(); } ... // Create the filter chain for this request ApplicationFilterChain filterChain = createFilterChain(request,servlet); // Call the filter chain for this request// This also calls the servlet's servicet() method try { String jspFile = wrapper.getJspFile(); if (jspFile != null) sreq.setAttribute(Globals.JSP_FILE_ATTR, jspFile); else sreq.removeAttribute(Globals.JSP_FILE_ATTR); if ((servlet != null) && (filterChain != null)) { filterChain.doFilter(sreq, sres); } sreq.removeAttribute(Globals.JSP_FILE_ATTR); } ... // Release the filter chain (if any) for this request try { if (filterChain != null) filterChain.release(); } ... // Deallocate the allocated servlet instance try { if (servlet != null) { wrapper.deallocate(servlet); } } ... // If this servlet has been marked permanently unavailable, // unload it and release this instance try { if ((servlet != null) && (wrapper.getAvailable() ==Long.MAX_VALUE)) { wrapper.unload(); } }
/** * The set of initialization parameters for this filter, keyed by * parameter name. */ private Map用HashMap存儲了初始化參數,它有get方法,增加屬性的方法是addInitParameter(String name, String value)。parameters = new HashMap ();
String filterClass = filterDef.getFilterClass(); ClassLoader classLoader = null; if (filterClass.startsWith("org.apache.catalina.")) classLoader = this.getClass().getClassLoader(); else classLoader = context.getLoader().getClassLoader(); ..... Class> clazz = classLoader.loadClass(filterClass); this.filter = (Filter) clazz.newInstance(); filter.init(this); return (this.filter);還是沒有什麼要說的。
private ArrayList filters = new ArrayList();
看到了把,數組形式來存放鏈條。
典型的責任鏈模式。
public void doFilter(ServletRaquest request, ServletResponse response,FilterChain chain) throws java.io.IOException, ServletException 在ApplicationFilterChain的doFilter方法中,它會將自己作為第三個參數傳遞給它。 我們看一個Filter實現類的例子 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // do something here ... chain.doFilter(request, response); }循環往復了
http://blog.csdn.net/dlf123321/article/details/40078583
等所有的Filter都執行完了,就是下面的代碼
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) { servlet.service((HttpServletRequest) request, (HttpServletResponse) response); } else { servlet.service(request, response); }
什麼時候Filter才算執行完了呢?
private Iterator iterator = null; .... if (this.iterator == null) this.iterator = filters.iterator(); // Call the next filter if there is one if (this.iterator.hasNext()) { //執行filter } ... //調用servlet的service的代碼塊 ...