tomcat java comp env,如何设置< Resource>在Tomcat 7中,这样我就不需要使用"java:/comp/env"在代码中?...

I am new to setting up JNDI resources and setting up JNDI resources in Tomcat.

I inherited a servlet application. It runs on a test server via WebLogic. The servlet application accesses its database resource in the following way:

ctx = new InitialContext();

ds = (javax.sql.DataSource)ctx.lookup("myDataBaseName");

conn = ds.getConnection();

When I tried that in a test JSP it doesn't work. I get

javax.naming.NameNotFoundException: Name myDataBaseName is not bound in this Context

However I was able to make the test JSP work if I altered the test JSP code thus:

ctx = new InitialContext();

Context envContext = (Context)ctx.lookup("java:/comp/env");

ds = (DataSource)envContext.lookup("myDataBaseName");

conn = ds.getConnection();

I have this entry in TOMCAT_HOME/conf/context.html ( I'm just using it as a dev environment on my box )

auth="Container"

type="javax.sql.DataSource"

driverClassName="oracle.jdbc.OracleDriver"

url="jdbc:oracle:thin:@blahblahblah:1521:database3"

username="joeuser"

password="password"

maxActive="20"

maxIdle="30"

maxWait="-1"/>

And I have this in my TOMCAT_HOME/conf/web.xml:

myDataBaseName

javax.sql.DataSource

Container

I read about 10 stackoverflow pages on related problems, but I am not familiar enough with setting up JNDI resources to abstract those solutions to my problem.

How can I change the way I have Tomcat 7 set up to allow the existing servlet application to access its database as "myDatabaseName"? I don't have the option of altering the code in the servlet application or altering the way WebLogic is set up on the test server.

I need to alter the way my copy of Tomcat 7 is set up on my computer ( for a dev environment ) to allow the servlet application to access its database in the style of the first chunk of quoted code at the top of this post.

解决方案

I see 2 possibilities here:

Implement a InitialContextFactory and make it available to tomcat (via system property), so that new InitialContext() gives you a context the lookup would work on.

or

Bind the data source to another place in the JNDI, which actually works although it is neither recommended nor documented (tomcat FM states the tree being read-only which only seems to apply to the subtree java:/comp/env):

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:/comp/env/myDataBaseName");

ctx.bind("myDataBaseName", ds);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值