【发布时间】:2015-04-11 08:06:34
【问题描述】:
/WEB-INF中jetty-web.xml的内容:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Array id="plusConfig" type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> <!-- add for jndi -->
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item> <!-- add for jndi -->
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
</Array>
<Call class="java.lang.System" name="setProperty">
<Arg>java.naming.factory.initial</Arg>
<Arg><Property name="java.naming.factory.initial" default="org.eclipse.jetty.jndi.InitialContextFactory"/></Arg>
</Call>
<Call class="java.lang.System" name="setProperty">
<Arg>java.naming.factory.url.pkgs</Arg>
<Arg><Property name="java.naming.factory.url.pkgs" default="org.eclipse.jetty.jndi"/></Arg>
</Call>
</Configure>
/WEB-INF中jetty-plus.xml的内容:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add plus Configuring classes to all webapps for this Server -->
<!-- =========================================================== -->
<Call class="org.eclipse.jetty.webapp.Configuration$ClassList" name="setServerDefault">
<Arg><Ref refid="Server" /></Arg>
<Call name="addAfter">
<Arg name="afterClass">org.eclipse.jetty.webapp.FragmentConfiguration</Arg>
<Arg>
<Array type="String">
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
</Array>
</Arg>
</Call>
</Call>
</Configure>
这是我在 Servlet 的 init() 方法中的调用代码:
Context _initCtx = new InitialContext();
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
它抛出的错误:
javax.naming.NameNotFoundException; remaining name 'env'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:449)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:551)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:117)
at javax.naming.InitialContext.lookup(Unknown Source)
我正在运行 vanilla GWT 2.7,并尝试在我的服务器代码中使用 JNDI 查找。 我还将 jetty-all-8.1.9.v20130131.jar 和 jetty-plus-8.1.9.v20130131.jar 附加到项目中,因为 GWT jar 中缺少一些 Context 和 JNDI 类。我尝试在 GWT 中验证 jetty 的版本,结果如下:
Server server = new Server(7070);
System.out.println(server.getVersion());
8.y.z-SNAPSHOT
我看到了可以通过其他方式配置 JNDI 的解决方案,但是由于我在 GWT 中运行,我只能使用 jetty-web.xml 和 jetty-plus.xml 文件。
在调试 servlet 时,我正在获取以下内容:
Context _envCtx = (Context) _initCtx.lookup("java:comp");
但不适用于:
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
Stacktrace 在上面。
我真的被困在这里了。那里有码头专家吗?
编辑 1:
添加 jetty-env.xml :
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='portal' class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DStest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="org.hsqldb.jdbcDriver">
<Set name="Url">jdbc:hsqldb:hsql://localhost</Set>
<Set name="User">sa</Set>
<Set name="Password"></Set>
</New>
</Arg>
</New>
</Configure>
这是我的 web.xml:
<resource-ref>
<description>Primary database</description>
<res-ref-name>jdbc/DSTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
仍然 _envCtx 为空,但以下例外。 _initCtx.lookup("java:comp") 确实返回 org.eclipse.jetty.jndi.NamingContext 实例。
Context _initCtx = new InitialContext();
Context _envCtx = (Context) _initCtx.lookup("java:comp/env");
结果
javax.naming.NameNotFoundException; remaining name 'env'
编辑 2:
我一定错过了其他东西(基本)....??!? 我将 jetty-env.xml 中的 1 行更改为:
<Arg><Ref refid="portal"/>jdbc/DSTest</Arg>
还有文档类型:
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
我找不到更适合 v8.X 的 .dtd 还是同样的问题。
【问题讨论】: