simple string value by JNDI in Java

风流意气都作罢 提交于 2019-11-29 18:00:42

In your web.xml use,

<env-entry>
<description>Sample env entry</description>
<env-entry-name>isConnected</env-entry-name>
<env-entry-type>java.lang.Boolean</env-entry-type><!--order matters -->
<env-entry-value>true</env-entry-value>
</env-entry>

In code,

try {
 Context initCxt =  new InitialContext();
 Boolean isConn =  (Boolean)initCxt.lookup("java:comp/env/isConnected");
 System.out.println(isConn.toString());
 // one could use relative names into the sub-context
 Context envContext = (Context) initCxt.lookup("java:comp/env");
 Boolean isConn2 = (Boolean)envContext.lookup("isConnected");
 System.out.println(isConn2.toString());
} catch (NamingException e) {
 e.printStackTrace();
}

Have a look here Naming service tutorial to get a good understanding of InitialContext and JNDI.

I don't know what's inside JndiDataSourceLookup().getDataSource("global/test") but by the name of it, it should return a DataSoruce not a string.

If your lookup is local, simply do

Context ctx = new InitialContext();
String s = (String) ctx.lookup("global/test");

or if you are in a javaee container,

@Resource(name="global/test")
String testString;

and finally in your ejb-jar.xml

<env-entry>
    <description>The name was explicitly set in the annotation so the classname prefix isn't required</description>
    <env-entry-name>global/test</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>StringValue</env-entry-value>
</env-entry>

Refer this link: http://tomee.apache.org/examples-trunk/injection-of-env-entry/README.html

Your configuration of context.xml, server.xml, and web.xml aren't gonna work.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!