Tomcat6 不修改server.xml和conf/context.xml配置数据源的问题.

2019-03-25 13:48|来源: 网路

网上说在web应用下建立META-INF/context.xml,但试了,没有成功.
我的context.xml内容如下
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource
      name="jdbc/myDS"
      type="javax.sql.DataSource"
      driverClassName="com.mysql.jdbc.Driver"
      maxIdle="2"
      maxWait="5000"
      username="root"
      password="root"
      url="jdbc:mysql://127.0.0.1:3306/mydb"
      maxActive="4"/>
</Context>
1.在tomcat_homt/conf/context.xml中加入上面内容,成功
2.建立mywebapp/META-INF/context.xml,提示不能找到jdbc/myDS
3.建立2,并且在web.xml中追加如下内容:
    <resource-ref>
      <res-ref-name>jdbc/mysql </res-ref-name>
      <res-type>javax.sql.DataSource </res-type>
      <res-auth>Container </res-auth>
    </resource-ref>
建立数据库链接时提示如下错误.
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

java.sql.SQLException: No suitable driver

注:驱动已放入tomcat_home/lib下.
访问数据源部分java代码
Context ctx = (Context) initCtx.lookup("java:comp/env");//2情况下这位置出错
Object obj = (Object) ctx.lookup("jdbc/mysql");
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();//3情况下,这里出错.

相关问答

更多
  • tomcat6 集群问题[2022-03-14]

    不可能吧? 必须要加jvmRoute的,这是在给你的Tomcat加一个负载均衡的名字。在apche中他就是通过这个tomcat1名字来寻找你的这个服务器的?你除非在apche中直接配置,都没有指定这个名字。 所以加他,这个服务器就不能不被访问因为根本找不到。你把所有的tomcat都加上,在apche中用名字去负载。
  • 在server.xml下配置你必需重启服务器才能生效,而context.xml配置保存后tomcat会自动加载无需重启
  • 更新只对任何与TomEE有这些罕见问题的人:我尝试在context.xml中创建数据源并在TomEE JAX-RS 1.5.0版本中部署而没有运气,它总是抛出空指针异常和数据源名称未找到。 最近我尝试使用TomEE JAX-RS版本1.6.0:我在context.xml中创建了我的数据源并使用此代码创建了一个简单的servlet Context initContext = new InitialContext(); Context envContext = (Context) initC ...
  • 确保在安装tomcat 6之前删除tomcat 7或任何其他版本的tomcat,这里是你如何删除它 sudo apt-get remove tomcat7-common 这里是你的链接https://askubuntu.com/questions/173981/installing-tomcat-7-on-ubuntu-server-12-04 make sure the tomcat 7 or any other version of tomcat is removed before installin ...
  • 您将应用程序从32位Hotspot JVM移动到64位Openjdk JVM。 在新服务器上,你有更少的RAM。 首先,我将尝试在新服务器上安装相同的32位Hotspot JVM,并查看是否仍然发生崩溃。 如果他们这样做,我会开始添加更多内存,并相应地调整xmx等。 You moved your app from a 32-bit Hotspot JVM to a 64-bit Openjdk JVM. And on the new server you have less RAM. First I wou ...
  • 只是好奇你的CATALINA_HOME和JAVA_HOME是否设置好了? 这些环境变量是运行Tomcat的先决条件。 转到开始 - >我的电脑(右键单击它) - >属性 - >高级 - >环境变量(按钮),然后将CATALINA_HOME设置为Tomcat和JAVA_HOME到JDK位置的路径,将JAVA_HOME \ bin添加到PATH变量中。 Ok, Solved this one myself. It turns out that "Load configuration failed" was a ...
  • server.xml用于服务器,而context.xml用于在该服务器上运行的应用程序。 在服务器上可能有几个context.xml文件(每个应用程序),但只有一个server.xml。 The server.xml is used for server and context.xml is for application that runs on that server. There may be several context.xml files (per application) on a serve ...
  • 根据Tomcat 6上下文文档 : 可以明确定义上下文元素: 仅当$ CATALINA_BASE / conf / [enginename] / [hostname] /中的应用程序不存在上下文文件时,才在应用程序文件内的/META-INF/context.xml中的单个文件中。 如果Web应用程序打包为WAR,则/META-INF/context.xml将复制到$ CATALINA_BASE / conf / [enginename] / [hostname] /并重命名以匹配应用程序的上下文路径。 一旦 ...
  • 这是我用来防止缓存的 // This will prevent catching of the results response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); This ...
  • 如果我从server.xml中删除 并仅在META-INF/context.xml定义它,那么我的代码工作正常 在两个地方都定义了时,它不起作用。 其次, 将您的类型更改为String ,而不是Integer I've got your code working IF I de ...