2015-08-27 44 views
3

我在IBM J9 VM版本pxa6470sr1-20120330_01(SR1)上使用Liberty概要文件v8.5.5.5(WebSphere Application Server 8.5.5.5/wlp-1.0.8.cl50520150221-0034) (en_US)JNDI查找在Liberty概要文件上失败

我安装了jndi功能...但无论我做什么,我都无法做简单的JNDI查找。

在我的server.xml

<jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato" /> 

我的代码...(这仅仅是一个几行的servlet)

Object jndiConstant = new InitialContext().lookup(
"schoolOfAthens/defaultAdminUserName"); 

但这种失败:

的javax。 naming.NameNotFoundException:名称schoolOfAthens在上下文中找不到“serverlocal:CELLROOT/SERVERROOT”。

该代码直接来自示例。

任何想法?

我在本地运行这一点,也试图在我的Bluemix帐户...同样的结果

+0

你能提供你配置的功能列表吗? – Alasdair

回答

1

好,我知道这个工作。我添加了一个资源引用到我的web.xml和看它是这样的:

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

同样对我的作品在8.5.5.6,没有0.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 
+0

欣赏回应。我也会尝试这种方式..... – James

相关问题