[英]Configure context lookup to find JNDI resource environment on Tomcat 8
I have a servlet that has a class that needs to reference a value from Tomcat 8's global JNDI resources. 我有一个servlet,它的类需要引用Tomcat 8的全局JNDI资源中的值。 I have defined the resource in the Tomcat server.xml file as such: 我已经在Tomcat server.xml文件中定义了资源,如下所示:
<GlobalNamingResources>
<Environment name="MAX_LANDMARK_RESULT" value="100000" type="java.lang.Integer" override="false"/>
</GlobalNamingResources>
I defined a resource link in the webapp context.xml: 我在webapp context.xml中定义了资源链接:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/pathname">
<ResourceLink name="MAX_QUERY" global="MAX_LANDMARK_QUERY" type="java.lang.Integer"/>
</Context>
I also defined the reference in the servlet web.xml: 我还在servlet web.xml中定义了引用:
<resource-env-ref>
<resource-env-ref-name>MAX_LANDMARK_QUERY</resource-env-ref-name>
<resource-env-ref-type>java.lang.Integer</resource-env-ref-type>
</resource-env-ref>
and I'm trying to reference it in my servlet like this: 我试图像这样在我的servlet中引用它:
try {
Context ct = new InitialContext();
Context ctx = (Context) ct.lookup("java:comp/env");
MAX_QUERY_SIZE = (Integer)ctx.lookup("MAX_QUERY");
} catch (NamingException ex) {
MAX_QUERY_SIZE = 99999;
}
I've triple checked how I set up the XML files, and they should be correct, so I'm certain that the issue comes from how I'm defining the ct.lookup("java:comp/env");
我已经三遍检查了如何设置XML文件,它们应该是正确的,所以我确定问题出在我定义ct.lookup("java:comp/env");
line. 线。 How can I define it so it finds the correct resource instead of throwing a NamingException? 我如何定义它,以便它找到正确的资源而不是抛出NamingException?
EDIT: So I have been diving into the debugger and checking my context, and MAX_QUERY is showing up as an inherited resource link that links to MAX_LANDMARK_QUERY. 编辑:所以我一直在深入调试器并检查我的上下文,并且MAX_QUERY显示为链接到MAX_LANDMARK_QUERY的继承资源链接。 Should I be doing something special when pulling that resource link so it references MAX_LANDMARK_QUERY in my code? 提取资源链接时,我是否应该做一些特别的事情,使其在我的代码中引用MAX_LANDMARK_QUERY?
For anyone else who is having the same problem, I found a solution. 对于遇到同样问题的其他人,我找到了解决方案。
<Environment>
should be defined inside the Tomcat context.xml file. <Environment>
应该在Tomcat context.xml文件中定义。 This is the only definition you have to make for the value. 这是您必须对值进行的唯一定义。
Inside your code, you should call it as such: 在您的代码内部,应这样调用:
Context ctx = new InitialContext();
value = (valuetype) ctx.lookup("java:/comp/env/valueName");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.