标签:tomcat 读书笔记 host engine valve
/** * Return the Context that would be used to process the specified * host-relative request URI, if any; otherwise return <code>null</code>. * * @param uri Request URI to be mapped */ public Context map(String uri);根据url来返回一个Context。
public StandardHost() { super(); pipeline.setBasic(new StandardHostValve()); }StandardHost并没有invoke方法,它需要调用ContainerBase的invoke方法。
//ContainerBase.java public void invoke(Request request, Response response)throws IOException, ServletException { pipeline.invoke(request, response); }另外StandardHostValve这个基础阀会调用StandardHost的map方法获得一个context容器。
public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException { .... StandardHost host = (StandardHost) getContainer(); //这里调用的是ContainerBase的map方法 最终会调用StandardHost的map方法(两个map方法没有关系 没有//复写 参数都不一样) Context context = (Context) host.map(request, true); .... }
在ContainerBase的start中有如下的代码
addDefaultMapper(this.mapperClass);看ConntainerBase
ContainerBase.java protected void addDefaultMapper(String mapperClass) { ...... // Instantiate and add a default Mapper Class<?> clazz = Class.forName(mapperClass); Mapper mapper = (Mapper) clazz.newInstance(); mapper.setProtocol("http"); addMapper(mapper); ...... }
StandardHost.java protected void addDefaultMapper(String mapperClass) { // 参数默认为 "org.apache.catalina.core.StandardHostMapper"; super.addDefaultMapper(this.mapperClass); }
public Container map(Request request, boolean update) { ... String uri = ((HttpRequest) request).getDecodedRequestURI(); Context context = host.map(uri); ... return (context); }在得到上下对象的时候需要一个往返过程,map 方法介绍两个参数,该方法是在 ContainerBase 中的。然后 ContainerBase 类又在它的子对象中查找合适的映射器并调用它的 map 方法。
public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException { .... StandardHost host = (StandardHost) getContainer(); //这里调用的是ContainerBase的map方法 最终会调用StandardHost的map方法(两个map方法没有关系 没有//复写 参数都不一样) Context context = (Context) host.map(request, true); .... HttpServletRequest hreq = (HttpServletRequest) request.getRequest(); String sessionId = hreq.getRequestedSessionId(); if (sessionId != null) { Manager manager = context.getManager(); if (manager != null) { Session session = manager.findSession(sessionId); if ((session != null) && session.isValid()) session.access(); //修改session的最后访问时间 } } context.invoke(request, response); //最后调用context的invoke }
private void applicationConfig() { ... synchronized (webDigester) { try { URL url = servletContext.getResource(Constants.ApplicationWebXml); ..... }
public URL getResource(String path) throws MalformedURLException { DirContext resources = context.getResources(); if (resources != null) { String fullPath = context.getName() + path; // this is the problem. Host must not be null String hostName = context.getParent().getName(); .... }看到最后一行的getParent大家都明白了吧。
public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException { .......... // Select the Host to be used for this Request StandardEngine engine = (StandardEngine) getContainer(); Host host = (Host) engine.map(request, true); //同样调用的是Containerbase的map //addDefaultMapper 怎么来的就不用说//了吧 if (host == null) { ((HttpServletResponse) response.getResponse()).sendError (HttpServletResponse.SC_BAD_REQUEST, sm.getString("standardEngine.noHost", request.getRequest().getServerName())); return; } // Ask this Host to process this request host.invoke(request, response); }
How tomcat works 读书笔记十三 Host和Engine
标签:tomcat 读书笔记 host engine valve
原文地址:http://blog.csdn.net/dlf123321/article/details/41543369