---
title: "Tomcat Context 学习备忘"
author: "gatusso52"
date: "2010-04-22T14:38:10Z"
tags: ["JDBC", "Microsoft", "SQL", "SQL Server", "Tomcat"]
categories: ["编程语言"]
slug: "tomcat-context"
draft: false
lastmod: "2010-04-22T14:38:10Z"
id: 650447
url: "https://gatusso52.iteye.com/blog/650447"
---

转自[http://www.blogjava.net/caiwen/archive/2007/04/03/108212.html](http://www.blogjava.net/caiwen/archive/2007/04/03/108212.html)

 

 

 

今天终于搞定Tomcat的Context了,conf/Context.xml是Tomcat公用的环境配置;若在Server.xml中增加&lt;Context path="/test" docBase="D:\\test" debug="0" reloadable="false"/&gt;的话,则myApp/META-INF/Context.xml就没用了(被server.xml中的Context替代),Jndi 数据源或其他资源可直接定在里面,如:    
    &lt;Context path="/frame" docBase="C:\\Java\\jboss-4.0.1sp1\\server\\default\\deploy\\webroot.war" debug="0" reloadable="false"&gt;  
      &lt;Resource name="UserTransaction" auth="Container"  
                                            type="javax.transaction.UserTransaction"  
                                            factory="org.objectweb.jotm.UserTransactionFactory"  
                                            jotm.timeout="60" /&gt;        
      &lt;Resource name="sharkdb" type="javax.sql.DataSource"                                        
                                      password="helloworld" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"  
                                      maxIdle="2" maxWait="5000" username="sa"  
                                      url="jdbc:microsoft:sqlserver://192.168.0.193:1433;DatabaseName=test\_cyber3;SelectMethod=cursor" maxActive="4"/&gt;  
                                        
     &lt;Resource name="cyberwaydb" type="javax.sql.DataSource"                                        
                                      password="helloworld" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"  
                                      maxIdle="2" maxWait="5000" username="sa"  
                                      url="jdbc:microsoft:sqlserver://192.168.0.193:1433;DatabaseName=test\_cyber3" maxActive="4"/&gt;  
         
      &lt;/Context&gt;;  
若在&lt;GlobalNamingResources&gt;中增加的JNDI或其他的,在App中需引用,才能使用,以下两种引用方式:  
  A:在WEB-INF/web.xml 中配置如下:  
&lt;resource-ref&gt;  
      &lt;description&gt;DB Connection&lt;/description&gt;  
      &lt;res-ref-name&gt;jdbc/TestDB&lt;/res-ref-name&gt;  
      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;  
      &lt;res-auth&gt;Container&lt;/res-auth&gt;  
  &lt;/resource-ref&gt;  
B:在yoursApp\\META-INF\\context.xml， 如下：  
&lt;?xml version='1.0' encoding='utf-8'?&gt;  
&lt;Context path="/yoursApp" docBase="yoursApp"   
debug="5"     
reloadable="true"   
crossContext="true"&gt;  
&lt;ResourceLink name="mysqlDBConPool" global="mysqlDBConPool" type="javax.sql.DataSource"/&gt;

&lt;/Context&gt;