java jndi教程,Java中JNDI的简单字符串值

How can i set simple string value in configuration of tomcat and then read in java application?

context.xml

server.xml

web.xml in application

global/test

java.lang.String

in my java application

public String getValue(){

return new JndiDataSourceLookup().getDataSource("global/test").toString();

}

When i Run tomcat, i see these errors...

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'global/test'; nested exception is javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].

javax.naming.NameNotFoundException: Name [global/test] is not bound in this Context. Unable to find [global].

解决方案

In your web.xml use,

Sample env entry

isConnected

java.lang.Boolean

true

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值