gpt4 book ai didi

gwt - 设置上下文 java :comp/env in Jetty running inside GWT 2. 7

转载 作者:行者123 更新时间:2023-12-04 11:37:05 24 4
gpt4 key购买 nike

/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");

堆栈跟踪在上面。

我真的被困在这里了。有 jetty 大师吗?

编辑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 仍然为空,但以下异常(exception)。 _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
还是一样的问题。

最佳答案

什么对我有用(假设您没有使用 SuperDevMode):

  • 添加 -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory运行 DevMode 时的 JVM 参数。 (在 extraJvmArgs 中用于 gwt-maven-插件)
  • 将 Jetty Plus 和 JNDI 的依赖项添加到您的 POM:
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-jndi</artifactId>
    <version>8.1.12.v20130726</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-plus</artifactId>
    <version>8.1.12.v20130726</version>
    <scope>provided</scope>
    </dependency>
  • jetty-web.xml 中配置 JNDI 条目:
    <?xml version="1.0"?>
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <New id="DataSource" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/XYZ</Arg>
    <Arg>
    <New class="org.postgresql.ds.PGSimpleDataSource">
    <Set name="user">X</Set>
    <Set name="password">Y</Set>
    <Set name="databaseName">Z</Set>
    <Set name="serverName">X</Set>
    <Set name="portNumber">A</Set>
    </New>
    </Arg>
    </New>

    <New class="org.eclipse.jetty.plus.jndi.EnvEntry">
    <Arg>config/ABC</Arg>
    <Arg type="java.lang.String">Value</Arg>
    </New>
    <!-- ... -->
    </Configure>

  • 就是这样。

    关于gwt - 设置上下文 java :comp/env in Jetty running inside GWT 2. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28446115/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com