浅析Tomcat之StandardEngine

本文详细解读了Tomcat的容器模型,包括其四个层次的结构及其在配置文件server.xml中的实现方式,着重介绍了顶层的Engine容器及其子容器Host的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tomcat的容器中,有着4个层次的Servlet容器,这4个类型的容器一般情况下是从属的关系.其中顶层的容器为Engine.这个Engine是Tomcat的整个Servlet引擎.它的子容器是Host,默认情况下Tomcat会有一个Engine,它所对应的阀是StandardEngineValve

首先,我们可以回头看下server.xml的配置.

<Engine name="Catalina" defaultHost="localhost">  
    <Realm className="org.apache.catalina.realm.LockOutRealm">  
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
               resourceName="UserDatabase"/>  
    </Realm>  
    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">  
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt"  
               pattern="%h %l %u %t "%r" %s %b" />  
    </Host>  
</Engine>  

   从配置中可以知道Service节点除了定义Connector,还定义了Engine,它里面还有一个Host.也就是说StandardService初始化的时候,就构造了Servlet引擎StandardEngine等一系列的容器层次.我们就看看Engine的构造方法.

public StandardEngine() {  
        super();  
        pipeline.setBasic(new StandardEngineValve());  
        /* Set the jmvRoute using the system property jvmRoute */  
        try {  
            setJvmRoute(System.getProperty("jvmRoute"));  
        } catch(Exception ex) {  
            log.warn(sm.getString("standardEngine.jvmRouteFail"));  
        }  
        // By default, the engine will hold the reloading thread  
        backgroundProcessorDelay = 10;  
  
    } 

 StandardEngine继承自ContainerBase自然也就有了pipeline对象和invoke方法.构造函数中设置的基础阀是StandardEngineValve.它是Engine特有的.

public final void invoke(Request request, Response response)  
    throws IOException, ServletException {  
  
    // Select the Host to be used for this Request  
    Host host = request.getHost();  
    if (host == null) {  
        response.sendError  
            (HttpServletResponse.SC_BAD_REQUEST,  
             sm.getString("standardEngine.noHost",  
                          request.getServerName()));  
        return;  
    }  
    if (request.isAsyncSupported()) {  
        request.setAsyncSupported(host.getPipeline().isAsyncSupported());  
    }  
  
    // Ask this Host to process this request  
    host.getPipeline().getFirst().invoke(request, response);  
  
} 

 上述代码是StandardEngineValve的invoke方法,从中我们可以知道它的作用只是把调用转发给了它的子容器Host,也就是他只起到了一个路由的作用.真正处理请求的还是在它的下级容器.

 

 

首发于泛泛之辈 - http://www.lihongkun.com/archives/148

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值