我在IBM J9 VM上使用Liberty Profile v8.5.5.5(WebSphere Application Server 8.5.5.5/wlp-1.0.8.cl50520150221-0034),版本为pxa6470sr1-20120330_01(SR1)(en_US)
我安装了jndi功能......但无论我做什么,我都无法进行简单的JNDI查找。
在我的server.xml中
<jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato" />
我的代码......(这只是几行的servlet)
Object jndiConstant = new InitialContext().lookup(
"schoolOfAthens/defaultAdminUserName");
但这失败了:
javax.naming.NameNotFoundException:在上下文“serverlocal:CELLROOT / SERVERROOT”中找不到名称schoolOfAthens。
代码直接来自一个例子。
有什么想法吗?
我在本地运行此功能,并且还试用了我的Bluemix帐户......结果相同
答案 0 :(得分:1)
Object obj2 = ctx.lookup("java:comp/env/schoolOfAthens/defaultAdminUserName");`
的web.xml
<resource-ref>
<description>Test Reference</description>
<res-ref-name>schoolOfAthens/defaultAdminUserName</res-ref-name>
<res-auth>Container</res-auth>
</resource-ref>
答案 1 :(得分:0)
对于我来说同样适用于8.5.5.6,手边没有.5但是应该以相同的方式工作。
这是我的server.xml:
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>servlet-3.1</feature>
<feature>jndi-1.0</feature>
<feature>localConnector-1.0</feature>
</featureManager>
<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
<applicationMonitor updateTrigger="mbean"/>
<webApplication id="JNDITest" location="JNDITest.war" name="JNDITest"/>
<jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato" />
</server>
和servlet代码(看看你也可以使用@Resource
注释而不是查找):
@WebServlet("/JNDIServlet")
public class JNDIServlet extends HttpServlet {
@Resource(lookup="schoolOfAthens/defaultAdminUserName")
String jndiVariable;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
InitialContext ctx = new InitialContext();
Object object = ctx.lookup("schoolOfAthens/defaultAdminUserName");
System.out.println("object: " + object);
System.out.println("jndiVariable: " + jndiVariable);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
带输出:
object: plato
jndiVariable: plato