tomcat源码 StandardService

在执行StandardServer的initInternal的时候会执行StandardService#init,然后会调到initInternal
protected void startInternal() throws LifecycleException {

    if(log.isInfoEnabled())
        log.info(sm.getString("standardService.start.name", this.name));
    setState(LifecycleState.STARTING);

    // Start our defined Container first
    if (engine != null) {
        synchronized (engine) {
            //引擎
            engine.start();
        }
    }

    synchronized (executors) {
        for (Executor executor: executors) {
            executor.start();
        }
    }

    //初始化Mapper组件,设置Host,Context,Wrapper的映射关系
    mapperListener.start();

    // Start our defined Connectors second
    synchronized (connectorsLock) {
        for (Connector connector: connectors) {
            try {
                // If it has already failed, don't try and start it
                if (connector.getState() != LifecycleState.FAILED) {
                    //初始化 连接相关的组件
                    connector.start();
                }
            } catch (Exception e) {
                log.error(sm.getString(
                        "standardService.connector.startFailed",
                        connector), e);
            }
        }
    }
}

Mapper组件的核心功能是提供请求路径的路由映射,根据某个请求路径通过计算得到相应的Servlet(Wrapper)
Mapper的映射包括Host容器、Context容器、Wrapper容器等的映射关系以及映射算法

一个service容器包含了N个Host容器的引用,然后每个Host会有N个Context容器的引用,最后每个Context容器包含N个Wrapper容器的引用。Wrapper容器会找到具体的servlet。

当tomcat启动稳定后,意味着这些映射都已经组织好,那么具体是如何查找对应容器的?

  • Host的匹配,直接对Mapper中的Host映射数组进行忽略大小写的二分搜索查找。
  • Context的匹配,对上面查找到的Host映射中的Context映射数组进行忽略大小写的二分搜索查找,这里有个比较特殊的情况是请求地址可以直接以Context名结束,例如http://tomcat.apache.org/tomcat-7.0-doc,另外一些则类似http://tomcat.apache.org/tomcat-7.0-do/index.html。另外,Context映射中的name对应的是Context容器的path属性。
  • Wrapper的匹配,首先,尝试使用精确匹配法匹配精确类型Servlet的路径;然后,尝试使用前缀匹配通配符类型Servlet;接着尝试使用扩展名匹配通配符类型Servlet;最后,匹配成默认Servlet。

Tomcat在处理请求时对请求的路由分发全由Mapper组件负责,请求通过Mapper找到最终的处理Servlet或资源。而在tomcat中会有两种类型的Mapper,它们作用的范围不同,因为称为全局路由映射和局部路由映射。

原文地址:https://www.cnblogs.com/grasp/p/10089722.html