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

问题:

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因为pathcontext标签下是不是指定了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

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,以下是在Tomcatcontext.xml文件配置JDBC连接池的步骤: 1. 打开Tomcat服务器context.xml文件,该文件位于Tomcat安装目录下的conf文件夹。 2. 在context标签添加Resource标签,指定JDBC连接池的配置信息,例如: ``` <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="username" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"/> ``` 其,name属性指定数据源的名称,username和password属性是连接数据库的用户名和密码,url属性指定连接数据库的URL,driverClassName属性是数据库驱动程序的类名。 3. 保存context.xml文件并重启Tomcat服务器,使配置生效。 如果您想在web.xml文件配置JDBC连接池,可以使用如下代码: ``` <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <resource-env-ref> <description>DB Connection</description> <resource-env-ref-name>jdbc/TestDB</resource-env-ref-name> <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type> </resource-env-ref> ``` 其,res-ref-name标签指定数据源的名称,resource-env-ref-name标签也需要设置为数据源的名称。具体的JDBC连接池的配置信息需要在Tomcat服务器的context.xml文件进行设置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孔明兴汉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值