Tomcat:javax.naming.NoInitialContextException [英] Tomcat: javax.naming.NoInitialContextException

查看:99
本文介绍了Tomcat:javax.naming.NoInitialContextException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat服务器上有一个带有Java Servlet的Web服务。
在我的Servlet中,我使用如下数据库池:

  envContext = new InitialContext(); 
DataSource ds =(DataSource)envContext.lookup( java:/ comp / env / jdbc / Database);
con = ds.getConnection();

对于初始化,我将其保存在web.xml中:

 < resource-ref> 
< description>数据库连接< / description>
< res-ref-name> jdbc / Database< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< / resource-ref>

然后是context.xml,这似乎是重要步骤此处:

 <?xml version = 1.0 encoding = UTF-8?> 
<!-将为每个Web应用程序加载此文件的内容->
< Context crossContext = true>

< ;!-监视资源的默认集合->
< WatchedResource> WEB-INF / web.xml< / WatchedResource>

<资源名称= jdbc /数据库 auth =容器
type = javax.sql.DataSource
maxActive = 100 maxIdle = 30 maxWait = 10000
用户名= user
密码=密码
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql:/ / localhost:3306 / configerror_db />
< / Context>

我读了很多其他有关此错误的问题,但我无法解决。



首先,我想解释一下,在使用Linux机器时,我不会出现此错误。因此,我在Windows计算机上的Eclipse中安装了相同的代码,我得到了此上下文环境错误。



其他答案说是要做到



让我知道那时候是否仍会停留。


I have a webservice with Java Servlets on a Tomcat server. In my Servlet I use a database pool like this:

envContext = new InitialContext();
DataSource ds = (DataSource) envContext.lookup( "java:/comp/env/jdbc/Database" );
con = ds.getConnection();

For initialisation I have this in my web.xml:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/Database</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

Then there is the context.xml which seems to be the important step here:

<?xml version="1.0" encoding="UTF-8"?>
 <!-- The contents of this file will be loaded for each web application -->
  <Context crossContext="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

   <Resource name="jdbc/Database" auth="Container"
        type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="user" 
        password="password" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/configerror_db"/>
</Context>

I read many other questions conserning this error, but I couldn't solve it.

First I want to explain, that I don't get this error when using my linux-machine. Hence I installed the same code in Eclipse on my Windows machine I get this context-environment error.

Other Answers say to do this.

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "<initialContextFactory>");
env.put(Context.PROVIDER_URL, "<url>");
env.put(Context.SECURITY_PRINCIPAL, "<user>");
env.put(Context.SECURITY_CREDENTIALS, "<password>");
ctx = new InitialContext(env);

But I don't know what the initialContextFactory is AND shouldn't the context.xml do exactly this? As I said on linux it works.

Can someone help me out here? What am I missing? I don't want to write the user and password in every file where I use a database connection. I thought hell yeah this is awesome. The credentials are just in the context.xml, but now on windows it is not working.

Thanks for any advice.

解决方案

Hi progNewbie,

I am working on Windows OS and Eclipse IDE as specially on same type of project for which are you looking.

you need to update your code at following places(3-Changes)...

1. Do Following Java Changes,

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
......
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/Database");
conn = ds.getConnection();

2. Configure Java Web-Application's web.xml likewise,

......
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/Database</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
......... 

3. Into Tomcat's server.xml file do following changes :

..................
<!-- Assume my application runs on localhost... -->
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

<Valve className=..../>

<Context docBase="...." path="..." reloadable="true" source=".....">

<!-- Assume i am using Oracle Database so that driverClass(below) Attribute's Value will be oracle.jdbc.driver.OracleDriver -->
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" 
    maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/Database" password="db-password" 
    type="javax.sql.DataSource" url="db-connection-url" username="db-user-name" 
/>

</Context>

</Host>
............

EDITED :

Let me know if still stuck up then.

这篇关于Tomcat:javax.naming.NoInitialContextException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆