"javax.naming.NameNotFoundException: Name jdbc is not bound in this Context"
这个错误网络上一大堆。大概知道这是应为WEB-INF里的web.xml文件中没有以下配置或者是配置错误:
<resource-ref>
首先,我们在Tomcat安装文件中找到 conf/context文件并打开, 在这里,我以我用的Oracle数据库
来配置。
而在WEB-INF文件下的 web.xml中,我们配置
<resource-ref></resource-ref>
例如:
在Tomcat中配置的name="jdbc/oracleds" ,而在 web.xml文件中配置的name="jdbc/oracle"都会引起
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
认真的看了自己的context.xml和web.xml中的配置,觉得它们正确无误。而错误依然不能消除,那么你看这篇文章就对了,也许它会是你的指路明灯。
你看一下自己的JSP文件,是否有这样一句:
DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/xinabai");
请将它改为:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/xinbai");
虽然无数本书和帖子告诉你,这两个方法相同。但是,很明显,前一种会在某种情况下产生Name jdbc is not bound in this Context的错误。
具体原因我不清楚,希望知道原因的高人可以告知一二。