您的位置:首页 > 运维架构 > Tomcat

Tomcat的工作流程&原理

2019-05-13 17:43 92 查看

相信大家在做JavaWeb项目的时候,都会用到这个服务器,有部分人和我一样,就是会使用,会配置,但是对于其具体是如何工作的还是不够了解,所以接下来就一起看看吧! 看完你就会明白了
首先假设一个请求:
http://localhost:8080/chen/index.jsp
1请求被发送到8080端口,被对应的Connector侦听到该请求并将获得的请求发送给Engine来处理,并等待来自Engine的回应并返回给客户

2Engine从Connector那里获得该对应的请求localhost:8080/chen/index.jsp,并进行匹配该对应的虚拟主机Host(其Engine中,可以配置多个虚拟主机)

3Engine匹配到名为localhost的Host(一般localhost为默认主机)

4localhost Host获得请求chen/index.jsp并匹配其所拥有的的Context(一个Context对应一个Web应用,一个Web应用由一个或者多个Servlet组成
)如下,是Tomcat中的web.xml配置,对于一个web应用,其默认自开启了JspServlet,还有ssi,cgi等Servlet

<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>

<!-- The mapping for the SSI servlet -->
<!--
<servlet-mapping>
<servlet-name>ssi</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
-->

<!-- The mapping for the CGI Gateway servlet -->

<!--
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
-->

5匹配到路径为path="/chen"的Context,并获得请求/chen/index.jsp,并在其mapping table中寻找对应的Servlet,Context匹配到URL PATTERN后缀名为.jsp的Servlet,对应于JSPServlet类*

<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>

6构造HttpServletRequest对象和HttpServletResponse对象,作为参数调用JspServlet的doGet或doPost方法

7Context将执行完毕之后的HttpServletResponse对象返回给Host

8Host将HttpServletResponse对象返回给Engine

9Engine将HttpServletResponse对象返回给Connector

10Connector将HttpServletResponse对象返回给客户浏览器

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: