Tomcat 配置文件笔记
tomcat 版本: 5.5.20
服务器主配置文件路径: {installDir}/conf/server.xml 。
服务器上下文配置文件路径: {installDir}/conf/context.xml 。
 
如:
D:/WAS/apache-tomcat-5.5.20/conf/server.xml
D:/WAS/apache-tomcat-5.5.20/conf/context.xml
D:/WAS/apache-tomcat-5.5.20/conf/tomcat-users.xml
D:/WAS/apache-tomcat-5.5.20/conf/web.xml
 
1.     配置方式一:
 
在 conf/server.xml 配置文件中配置 <Rescource> 和 <RescourceLink> 方式。
 
<?xml version="1.0" encoding="UTF-8"?>
<Server
    port="8006"
    shutdown="111">
  <Listener className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <GlobalNamingResources>
    <Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      pathname="conf/tomcat-users.xml"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
    <Resource
      name="xzg/orcl10gr2"
      type="javax.sql.DataSource"
      password="tiger"
      driverClassName="oracle.jdbc.OracleDriver"
      maxIdle="2"
      maxWait="5000"
      username="scott"
      url="jdbc:oracle:thin:@xizhiguang:1521:orcl"
      maxActive="20"/>
    <Resource
      name="xzg/mysql"
      type="javax.sql.DataSource"
      password="javaduke"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="javauser"
      url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"
      maxActive="20"/>
  </GlobalNamingResources>
[x1]   <Service
      name="Catalina">
    <Connector
        port="8080"
        redirectPort="8443"
        minSpareThreads="25"
        connectionTimeout="20000"
        uRIEncoding="GBK"
        maxSpareThreads="75"
        maxThreads="150">
    </Connector>
    <Connector
        port="8009"
        redirectPort="8443"
        protocol="AJP/1.3">
    </Connector>
    <Engine
        defaultHost="localhost"
        name="Catalina">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
      <Host
          appBase="webapps"
          name="localhost">
        <Context
            docBase="DBTest"
            crossContext="true"
            path="/DBTest"
            privileged="true"
            reloadable="true">
<ResourceLink
                global="xzg/orcl10gr2"
                auth="Container"
                type="javax.sql.DataSource"
                name="xzg/orcl10gr2"/>
            <ResourceLink
                global="xzg/mysql"
                auth="Container"
                type="javax.sql.DataSource"
                name="xzg/mysql"/>[x2]           <WatchedResource>D:/was/apache-tomcat-5.5.20/conf/context.xml</WatchedResource>
        </Context>
      </Host>
    </Engine>
  </Service>
</Server>
 
 
在 <Host></Host> 标记内加上下文标记 <Context></Context> ,在 <Context></Context> 内加 <Resource></Resource> 或者 <ResourceLink></ResourceLink>标记,来定义数据源配置。
 
 
注意:
    通过实际观察发现Tomcat 服务器启动和停止的时候都会校验 conf/server.xml 文件。
如,在运行过程中,修改了 server.xml 配置文件,填入了错误信息,停止服务器的时候就会导致服务器抛出异常停不下来服务器,修改正确后方可停止。 配置文件的错误会导致服务器启动不了。
 
2.     配置方式二:
 
在 conf/server.xml 中配置 <Rescource>,在conf/content.xml 中配置全局资源引用 <RescourceLink> 方式(注意:Conf 下的 xml 配置文件是全局配置文件,在 Tomcat 服务器启动的时候会一一加载)。
 
2.1   server.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<Server
    port="8006"
    shutdown="111">
  <Listener className="org.apache.catalina.core.AprLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <GlobalNamingResources>
    <Environment
      name="simpleValue"
      type="java.lang.Integer"
      value="30"/>
    <Resource
      auth="Container"
      description="User database that can be updated and saved"
      name="UserDatabase"
      type="org.apache.catalina.UserDatabase"
      pathname="conf/tomcat-users.xml"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
    <Resource
      name="xzg/orcl10gr2"
      type="javax.sql.DataSource"
      password="tiger"
      driverClassName="oracle.jdbc.OracleDriver"
      maxIdle="2"
      maxWait="5000"
      username="scott"
      url="jdbc:oracle:thin:@xizhiguang:1521:orcl"
      maxActive="20"/>
    <Resource
      name="xzg/mysql"
      type="javax.sql.DataSource"
      password="javaduke"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="javauser"
      url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"
      maxActive="20"/>
  </GlobalNamingResources>
  <Service
      name="Catalina">
    <Connector
        port="8080"
        redirectPort="8443"
        minSpareThreads="25"
        connectionTimeout="20000"
        uRIEncoding="GBK"
        maxSpareThreads="75"
        maxThreads="150">
    </Connector>
    <Connector
        port="8009"
        redirectPort="8443"
        protocol="AJP/1.3">
    </Connector>
    <Engine
        defaultHost="localhost"
        name="Catalina">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
      <Host
          appBase="webapps"
          name="localhost">
      </Host>
    </Engine>
  </Service>
</Server>
 
2.2  context.xml
 
<!-- The contents of this file will be loaded for each web application -->
<Context privileged='true'
         reloadable="true">
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
 
    <ResourceLink
        global="xzg/orcl10gr2"
        auth="Container"
        type="javax.sql.DataSource"
        name="xzg/orcl10gr2"/>
    <ResourceLink
        global="xzg/mysql"
        auth="Container"
        type="javax.sql.DataSource"
        name="xzg/mysql"/>
</Context>
 
3.     配置方式三:
 
又如,建一个应用的发布目录 {installDir}/webapps/test,相应的在 {installDir}/conf/Catalina/localhost/ 目录下建立一个针对发布目路的上下文配置 test.xml 文件。Server.xml 配置文件中只需定义全局的 <Rescource> 资源引用。
 
如: D:/WAS/apache-tomcat-5.5.20/conf/Catalina/localhost/test.xml, 文件的内容如下。
 
<?xml version="1.0" encoding="UTF-8"?>
<Context
    crossContext="true"
    privileged="true"
    reloadable="true">
    <ResourceLink
        global="xzg/orcl10gr2"
        auth="Container"
        type="javax.sql.DataSource"
        name="xzg/orcl10gr2"/>
    <ResourceLink
        global="xzg/mysql"
        auth="Container"
        type="javax.sql.DataSource"
        name="xzg/mysql"/>  <WatchedResource>D:/was/apache-tomcat-5.5.20/conf/context.xml</WatchedResource>
</Context>