网络小知识

Tomcat的体系结构
<?xml version="1.0" encoding="UTF-8"?><!--顶层类元素,可以包含多个Service--><Server port="8005" shutdown="SHUTDOWN"><Listener className="org.apache.catalina.startup.VersionLoggerListener" ><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" ><Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" ><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" ><Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" ><GlobalNamingResources><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><!--顶层类元素,可包含一个Engine(container),多个connector--><Service name="Catalina"><!--连接器类元素,代表通信接口--><Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" ><Connector port="8009" protocol="AJP/1.3" redirectPort="8443" ><!--容器类元素,为特定的service组件处理客户请求--><Engine name="Catalina" defaultHost="localhost"><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></Server>

Tomcat的两个核心组件
Connector
Containtner

Tomcat处理一个Http请求

用户在浏览器中输入网址localhost:8080/test/index.jsp,请求被发送到本机端口8080,被在那里监听的Coyote HTTP/1.1 Connector获得;
Connector把该请求交给它所在的Service的Engine(Container)来处理,并等待Engine的回应;
Engine获得请求localhost/test/index.jsp,匹配所有的虚拟主机Host;
Engine匹配到名为localhost的Host(即使匹配不到也把请求交给该Host处理,因为该Host被定义为该Engine的默认主机),名为localhost的Host获得请求/test/index.jsp,匹配它所拥有的所有Context。Host匹配到路径为/test的Context(如果匹配不到就把该请求交给路径名为“ ”的Context去处理);
path=“/test”的Context获得请求/index.jsp,在它的mapping table中寻找出对应的Servlet。Context匹配到URL Pattern为*.jsp的Servlet,对应于JspServlet类;
构造HttpServletRequest对象和HttpServletResponse对象,作为参数调用JspServlet的doGet()或doPost(),执行业务逻辑、数据存储等;
Context把执行完之后的HttpServletResponse对象返回给Host;
Host把HttpServletResponse对象返回给Engine;
Engine把HttpServletResponse对象返回Connector;
Connector把HttpServletResponse对象返回给客户Browser。




