2013-03-16 60 views
0

定义在server.xml中的环境变量如下ResourceLink元素是我使用Apache的Tomcat的7.0.35.I不工作

<GlobalNamingResources> 

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

    </GlobalNamingResources> 

我使用ResourceLink元素在侧我上下文元素在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> 

当我尝试使用

Context initCtx = new InitialContext(); 
     String configPath = (String)initCtx.lookup("sam"); 

它扔得到这个valuue代码

我该如何解决这个问题?

回答

1

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

修改的context.xml

<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. 

因为我不知道你这样做的目的,我不能帮你了。 :-(

按照此链接查看更多细节的Apache给了很好的榜样

希望这有助于 http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

1

Tomcat的JNDI具有默认名称空间。 “的java:comp/env的”

修改代码,要么

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"); 
相关问题