【0】README
0.0)本文中源代码的背景,参见 tomcat(9)session管理
0.1)本文主要以图片的形式分析他们大致的调用过程;
0.2)HttpConnector == org.apache.catalina.connector.http.HttpConnector; 而StandardContext == org.apache.catalina.core.StandardContext;
0.3)应用程序的source code 如下:
public final class Bootstrap {public static void main(String[] args) {System.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Wrapper wrapper1 = new SimpleWrapper();wrapper1.setName("Session");wrapper1.setServletClass("SessionServlet");Context context = new StandardContext();context.setPath("/myApp");context.setDocBase("myApp");context.addChild(wrapper1);context.addServletMapping("/myApp/Session", "Session");LifecycleListener listener = new SimpleContextConfig();((Lifecycle) context).addLifecycleListener(listener);Loader loader = new WebappLoader();context.setLoader(loader);connector.setContainer(context);Manager manager = new StandardManager();context.setManager(manager);try {connector.initialize(); // highlight line.((Lifecycle) connector).start(); // highlight line.((Lifecycle) context).start(); // highlight line.// make the application wait until we press a key.System.in.read();((Lifecycle) context).stop();}catch (Exception e) {e.printStackTrace();}}
}
【1】HttpConnector.initialize()
【2】HttpConnector.start()
【3】StandardContext.start()
/*** Load and initialize all servlets marked "load on startup" in the* web application deployment descriptor.** @param children Array of wrappers for all currently defined* servlets (including those not declared load on startup)*/public void loadOnStartup(Container children[]) { // org.apache.catalina.core.StandardContext.loadOnStartUp().// Collect "load on startup" servlets that need to be initializedTreeMap map = new TreeMap();for (int i = 0; i < children.length; i++) {Wrapper wrapper = (Wrapper) children[i];int loadOnStartup = wrapper.getLoadOnStartup();if (loadOnStartup < 0)continue;if (loadOnStartup == 0) // Arbitrarily put them lastloadOnStartup = Integer.MAX_VALUE;Integer key = new Integer(loadOnStartup);ArrayList list = (ArrayList) map.get(key);if (list == null) {list = new ArrayList();map.put(key, list);}list.add(wrapper);}// Load the collected "load on startup" servletsIterator keys = map.keySet().iterator();while (keys.hasNext()) {Integer key = (Integer) keys.next();ArrayList list = (ArrayList) map.get(key);Iterator wrappers = list.iterator();while (wrappers.hasNext()) {Wrapper wrapper = (Wrapper) wrappers.next();try {wrapper.load();} catch (ServletException e) {log(sm.getString("standardWrapper.loadException",getName()), e);// NOTE: load errors (including a servlet that throws// UnavailableException from tht init() method) are NOT// fatal to application startup}}}}