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

Tomcat文件夹下conf文件夹中的context.xml文件存在的目的是什么

2017-12-07 14:43 686 查看
问题:

Tomcat文件夹下conf文件夹中的context.xml文件存在的目的是什么?



回答:

首先,你要明白一点,tomcat的工作原理是什么?也是一个Java程序,是一个网络服务。

那么,webapp是如何嵌入的?反射。

webapp是如何加载的?webapp的入口?是servlet和过滤器以及监听器。这是一个webapp的入口。

一个webapp又被称为什么?在tomcat中被抽象成什么对象?是ServletContext。

假如,你的tomcat下部署了两个webapp。最终你的tomcat中是不是管理了两个ServletContext对象。每一个对象代表一个webapp。

现在的问题是,为什么建立2个,而不是3个?tomcat启动之后为什么会建立这样两个对象?Tomcat文件夹的conf文件夹中的server.xml里面配置了context标签。当tomcat启动的时候,会读取这个文件。发现配置了2个context,那么就应该建立两个ServletContext对象。

 

为什么不同的url会跑到不同的ServeltContext?因为path,context标签下是不是指定了path,这样,在tomcat内部就是维护了一个map。每个context对应一个path,这样,tomcat接受到请求,读取url,就会把这个请求交给指定的ServletContext。但是,ServletContext粒度很大,一个ServeltContext下管理了很多个serlet,还需要进一步路由。这个路由规则由谁指定?是不是每个ServletContext内部,还会维护一个路由,这个路由就是每个项目的WEB-INF文件夹下的web.xml。也就是说    项目里的web.xml  是来管理自己项目中的东西的。某个具体项目的WEB-INF文件夹下的web.xml的范围是这个项目ServletVontext的内部级别。

 

 

WEB-INF文件夹下的web.xml是不是配置了不同的sevlet的mapping,全部由这个文件来指定。那么,tomcat为什么知道是这个文件?这些都需要tomcat这个java程序来读取、来分配、来指定规则,因为Tomcat文件夹下的conf文件夹中的context.xml中指定了这个web.xml的路径,如图:(context.xml文件中代码)

 <?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.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
</Context>

 

 

假如你把红线那一行改了,你改成web2.xml。那么,你的webapp中也要相应的改成web2.xml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐