无法使用 Java JNDI 上下文查找访问对象
我正在运行 Tomcat6 并且想要从我的 Servlet 访问数据源。但我得到
javax.naming.OperationNotSupportedException: can''t generate an absolute name for this namespace
at org.apache.naming.NamingContext.getNameInNamespace(NamingContext.java
:772)
My context.xml is under HomeController/META-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/HomeController">
<Resource name="jdbc/HomeController" auth="Container" type="javax.sql.DataSource" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
acquireIncrement="5"
username="client"
password="1234"
jdbcUrl="jdbc:sqlserver://192.168.1.5:1433;databaseName=myDB;autoReconnect=true" />
</Context>
我也将相同的 context.xml 重命名为我的 webapp“HomeController”并放置在 TOMCAT_HOME/conf/Catalina/localhost/HomeController.xml 下。
我的 web.xml 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Home Controller</display-name>
<servlet>
<servlet-name>HomeController</servlet-name>
<servlet-class>com.home.controller.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
<url-pattern>/HomeController</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/HomeController</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
在我的 Servlet 中,我有以下内容:
Connection conn = null;
Context ctx = null;
java.sql.Statement stmt = null;
try {
ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env"); //<--FAILS HERE
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmscribeWS");
conn = ds.getConnection();
DatabaseMetaData mt = conn.getMetaData();
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
我安装了 Tomcat6 的全新副本,并将 sqljdbc4.jar 放在 TOMCAT_HOME/lib 下。 我的 HomeController/WEB-INF/lib 下没有 JAR。
在我的 Servlet 中执行“(Context) ctx.lookup("java:comp/env"); 时失败。
有人知道为什么检索对象时失败吗?
I'm running Tomcat6 and want to access the datasource from my Servlet. But I'm getting
javax.naming.OperationNotSupportedException: can''t generate an absolute name for this namespace
at org.apache.naming.NamingContext.getNameInNamespace(NamingContext.java
:772)
My context.xml is under HomeController/META-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/HomeController">
<Resource name="jdbc/HomeController" auth="Container" type="javax.sql.DataSource" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
acquireIncrement="5"
username="client"
password="1234"
jdbcUrl="jdbc:sqlserver://192.168.1.5:1433;databaseName=myDB;autoReconnect=true" />
</Context>
I also have the same context.xml renamed to my webapp "HomeController" and placed under TOMCAT_HOME/conf/Catalina/localhost/HomeController.xml.
My web.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Home Controller</display-name>
<servlet>
<servlet-name>HomeController</servlet-name>
<servlet-class>com.home.controller.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
<url-pattern>/HomeController</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/HomeController</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
In my Servlet I have this:
Connection conn = null;
Context ctx = null;
java.sql.Statement stmt = null;
try {
ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env"); //<--FAILS HERE
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmscribeWS");
conn = ds.getConnection();
DatabaseMetaData mt = conn.getMetaData();
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
I installed a fresh copy of Tomcat6 and placed the sqljdbc4.jar under TOMCAT_HOME/lib.
I have no JARS under my HomeController/WEB-INF/lib.
It fails when executing "(Context) ctx.lookup("java:comp/env"); " in my Servlet.
Anyone knows why it fails when retrieving object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样尝试一下。
您正在尝试查找资源引用,但尚未指定实际引用,这就是无法生成绝对名称的原因。
Try it like this.
You are attempting to lookup a resource reference, but have not specified the actual reference, which is why an absolute name cannot be generated.