Java Tomcat中的ResourceLink元素不工作

Java Tomcat中的ResourceLink元素不工作,java,jakarta-ee,tomcat7,jndi,Java,Jakarta Ee,Tomcat7,Jndi,我使用的是apache-tomcat-7.0.35。我在server.xml中定义了一个环境变量,如下所示 <GlobalNamingResources> <Environment name="sam" value="D:\AppServers\apache-tomcat-7.0.35\conf\sample.xml" type="java.lang.String" override="tr

我使用的是apache-tomcat-7.0.35。我在server.xml中定义了一个环境变量,如下所示

<GlobalNamingResources>

        <Environment name="sam" 
                 value="D:\AppServers\apache-tomcat-7.0.35\conf\sample.xml"
                 type="java.lang.String" override="true"/>

  </GlobalNamingResources>
它的抛出
javax.naming.NameNotFoundException.javax.naming.NameNotFoundException:Name[sam]不在此上下文中绑定。找不到[sam]。


如何更正此问题?

如果我没有错,您不需要修改server.xml

修改的context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Practice_1" docBase="/Practice_1"
    crossContext="true" reloadable="true" debug="1">
<ResourceLink name="sam" global="sam" type="java.lang.String"/>

</Context>
<Resource auth="Container"            java.naming.factory.initial="org.jnp.interfaces.NamingContextFactory" 
factory="de.example.CXIResourceLocator"
name="bean/CXIResourceLocator"
type="de.example.Bean"/>

web.xml

<resource-env-ref>
    <description>
        Connection pooling.
    </description>
    <resource-env-ref-name>bean/CXIResourceLocator</resource-env-ref-name>
    <resource-env-ref-type>
        de.example.Bean
    </resource-env-ref-type>
</resource-env-ref>

bean/CXIResourceLocator should match in both context and web xmls.

连接池。
bean/CXIResourceLocator
de.example.Bean
bean/CXIResourceLocator应该在上下文和web XML中匹配。
既然我不知道你这样做的目的,我就帮不了你了-(

请遵循此链接了解更多详细信息。Apache提供了一个很好的示例

希望这有帮助。

tomcat jndi具有默认名称空间“java:comp/env”

将代码修改为

Context initCtx = new InitialContext();
String configPath = (String)initCtx.lookup("java:comp/env/sam");

Context initCtx = new InitialContext();
String configPath = (String)initCtx.lookup("java:comp/env/sam");
Context initCtx = new InitialContext();
Context rootCtx = (Context) initCtx.lookup("java:comp/env");
String configPath = (String)rootCtx.lookup("sam");