gpt4 book ai didi

java - Tomcat JNDI 查找问题

转载 作者:行者123 更新时间:2023-11-30 09:46:06 25 4
gpt4 key购买 nike

我正在尝试读取 Tomcat 中的属性值。我的计划是使用 System.getProperty("LOGPATH") 访问该值,其中 LOGPATH 是我的属性的名称。但是我没有找到在 Tomcat 中设置系统属性值的方法。

有什么方法可以在 Tomcat 中设置系统属性吗?

由于我没有获得任何关于设置系统属性的文档,我想到了使用 JNDI 访问该值。所以我添加了以下条目

<Environment name="LOGPATH" type="java.lang.String" value="c:/temp" />

之后

<GlobalNamingResources>

server.xml 文件中。

我用来查找JNDI的代码是

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

String path = (String) envCtx.lookup("LOGPATH");

当我执行上面的代码时,我收到了以下错误信息。

javax.servlet.ServletException: Name LOGPATH is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)

然后我在我的 web.xml 中添加了一个条目

 <resource-env-ref>
<resource-env-ref-name>LOGPATH</resource-env-ref-name>
<resource-env-ref-type>java.lang.String</resource-env-ref-type>
</resource-env-ref>

现在错误信息改为

javax.naming.NamingException: Cannot create resource instance
org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:99)

我现在做错了什么?感谢您的帮助。

最佳答案

您还需要在网络应用的上下文中有一个资源链接。上下文通常放在 META-INF/context.xml 中,看起来像这样:

<Context>
<Resource name="LOGPATH" global="LOGPATH" type="java.lang.String" />
</Context>

这“授予”网络应用程序查看特定 Environment 值的权利。

至于设置系统属性,只需在 tomcat/conf/catalina.properties 中添加一行,如下所示:

LOGPATH=C:/temp

请注意,系统属性可用于所有 Web 应用程序,其中 JNDI 条目由每个 Web 应用程序控制。

关于java - Tomcat JNDI 查找问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7304586/

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