Tomcat的请求处理整体过程

一:Tomcat的各个组件和功能介绍

      1.  核心组件是Connector(连接器)组件和Container(容器)组件组成.一个Container对应多个Connector组件构成Service.

      2. Connector组件负责在服务端处理客户端连接,包括了接收客户端连接和接收客户端的消息报文,和消息报文的解析工作.

      3. Container组件负责对客户端的请求逻辑进行处理,然后返回给客户端.Container是包含4个级别的容器.

      4. 可以根据Tomcat的配置文件看到组件的包含关系.server.xml.(位于conf目录下的).Tomcat8.0.43的版本.

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

   5.  Server组件

     是Tomcat的顶级组件(位于最外层嘛),代表Tomcat的运行实例.一个JVM只能包含一个Server.也包含多个Listener组件.

  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    看一下Eclipse里面创建一个JavaWEB项目,然后New Server后,将项目添加到Server里面后,运行即可,观察一下Server.

    (Eclipse里面每次创建一个新的Server后,Server实例如下.里面的那个server.xml就是当前的配置信息.)

   

    6.  Service组件

        (Service是服务的抽象),代表请求从接受到处理各个阶段的集合,接受客户端消息的Connector组件和处理客户端请求处理的Engine组件和Server组件的对应关系是1对多的.Service组件还包含Executor组件,就是线程池嘛,可以看配置文件里面的那个tomcatThreadPool.为组件提供线程池来处理执行任务.(一个Service包含多个Connector原因是客户端使用不同通信协议的客户端,和一个Engine组件和多个Executor组件).

7. Connnector组件

     Connector组件负责接受客户端连接并接受消息报文的,消息报文经有它解析后发往消息处理组件Engine.一种客户端通信协议对应一个Connector组件,目前Tomcat包含两个分别是包含HTTP协议的和AJP协议的,可以server.xml的配置文件.一目了然.

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

      当然内部实现根据网络IO会分为阻塞IO和非阻塞IO的,不同的实现方式不同.(BIO/NIO).

8.  Tomcat内部的4个级别的容器.分别是Engine,Host,Context,Wrapper.先简单介绍一下Engine组件包含的其他组件吧.

     Listener组件:Tomcat的生命周期里面负责监听Engine容器的相关工作的监听器.

     AccessLog组件:客户端的访问日志,所有的客户端访问都会被记录的.

     Cluster组件: 提供Tomcat的集群,将Engine容器中数据同步其他实例上.

     Pipeline组件:Engine容器对请求进行处理的管道.

     Realm组件: 提供了Engine容器级别的用户-密码-权限的数据.

9. Host组件.

    存放Web应用的抽象,代表虚拟主机,localhost和各种域名映射,在Windows的hosts下映射.通俗的说是当前域名下的.包含组件如下.

     Context组件:Web应用的抽象.    

     Listener组件:Tomcat的生命周期里面负责监听Host容器的相关工作的监听器.

     AccessLog组件:客户端的访问日志,所有的客户端访问都会被记录的.

     Cluster组件: 提供Tomcat的集群,将Host容器中数据同步其他实例上.

     Pipeline组件:Host容器对请求进行处理的管道.

     Realm组件: 提供了Host容器级别的用户-密码-权限的数据.

10. Context组件.

     Conext组件是Web应用的抽象,我们自己开发的Web项目部署到Tomcat后运行就会转换为一个Context对象.部署一个项目到Tomcat就对应一个Context对象.

     例如下面的一个.(上文的那个serevr.xml是没有这个Context容器的,因为没有部署过项目的干净配置文件).

<Context docBase="JdbcWeb" path="/JdbcWeb" reloadable="true" source="org.eclipse.jst.jee.server:JdbcWeb"/>

   不同于Host组件的几个组件简单介绍.

   Loader组件,Web应用加载器,用于加载Web应用的资源,它保证不同Web应用之间的资源隔离.

   Manager组件,会话管理器,用于管理对应Web容器的会话.

   Mapper 组件,Servlet映射器,它属于Context内部的路由映射器, 只负责该Context容器的路由导航.

   Wrapper组件,Context的子容器.

11. Wrapper组件.

     对应的是Servlet,一个Wrapper组件对应一个Servlet.

     Servlet组件,Servlet就是Web应用开发常用的Servlet,我们会在Servlet中处理逻辑即可.

     ServletPool组件:Servlet对象池,实现SingleThreadModel接口时,实现可以产生单例.

     Pipeline组件,Wrapper容器对请求进行处理的管道.

二:Tomcat处理客户端请求的整体流程.

     面试题:给定一个客户端访问的URL:http://localhost:8080/TestWeb/index.jsp.详细说一下该请求的处理流程.

     1. 首先是请求发送给本机8080,被在那里侦听HTTP/Connector获得.

     2. Connector将该请求发给它本身所在的 Service所在的Engine来处理,并等待Engine来回应.

     3. Engine获得请求localhost:8080/TestWeb/index.jsp,匹配它所有的虚拟主机Host.(如果匹配不到也把该请求交给这个Host,这个是Engine的默认主机)

     4. Engine匹配别名为localhsot Host.

     5. localhost Host获得请求/TestWeb/index.jsp,匹配它所拥有的所有Context.

     6. Host匹配到路径为/TestWeb的context,(如果没有匹配到,就交给请求路径名为“”的context处理).

     7. path="/test"的Context获得请求/index.jsp,在它的mapping table寻找对应的Servlet.

     8. Context匹配到URL PATTERN为*.jsp的Servlet的,对应于JspServlet类.

     9. 构造HttpServletRequest对象和HttpServletResponse对象作为参数,调用JspServlet的doGet()方法和doPost()方法.

     10. Context把执行完后的返回HttpServletResponse对象返回给Host.

     11. Host把HttpServletResponse对象返回给Engine. 

     12. Engine把HttpServletResponse对象返回给Connector.

     13. Connector把HttpServletResponse对象返回给浏览器的Browser.

     至此Tomcat的处理请求的整体流程就简单介绍完了.

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道之简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值