请教:关于tomcat中配置JNDI的问题。

下面是错误提示。下面还有我的TOMCAT配置文件。(是通过tomcat图形页面生成的)
请各位 。小弟(本人人品低连代码都欺负人)。谢谢。


HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /jndiTest.jsp:10

7: <%
8:
9: Context initCtx = new InitialContext();
10: DataSource ds = (DataSource)initCtx.lookup("java:comp/env/dbc/Test");
11:
12: Connection conn = ds.getConnection();
13: conn.close();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.ServletException: Name dbc is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.jndiTest_jsp._jspService(jndiTest_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.naming.NameNotFoundException: Name dbc is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:769)
org.apache.naming.NamingContext.lookup(NamingContext.java:139)
org.apache.naming.NamingContext.lookup(NamingContext.java:780)
org.apache.naming.NamingContext.lookup(NamingContext.java:139)
org.apache.naming.NamingContext.lookup(NamingContext.java:780)
org.apache.naming.NamingContext.lookup(NamingContext.java:152)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:136)
javax.naming.InitialContext.lookup(InitialContext.java:351)
org.apache.jsp.jndiTest_jsp._jspService(jndiTest_jsp.java:55)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


tomcat配置文件
---------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/Test"
type="javax.sql.DataSource"
password="tiger"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="scott"
url="jdbc:oracle:thin:@10.164.11.226:1521:dbserver"
maxActive="4"/>
</GlobalNamingResources>
<Service
name="Catalina">
<Connector
port="8090"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150">
</Connector>
<Connector
port="8009"
redirectPort="8443"
protocol="AJP/1.3">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">
</Host>
</Engine>
</Service>
</Server>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Tomcat配置JNDI,你可以按照以下步骤进行操作: 1. 打开Tomcat配置文件`tomcat/conf/server.xml`。 2. 在`<GlobalNamingResources>`元素配置全局资源,例如数据库连接池。例如,你可以添加一个`<Resource>`元素来定义一个连接池: ```xml <GlobalNamingResources> <Resource name="jdbc/myDataSource" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="your_username" password="your_password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/your_database"/> </GlobalNamingResources> ``` 在上面的示例,我们定义了一个名为`jdbc/myDataSource`的资源,它是一个`javax.sql.DataSource`类型的数据库连接池。 3. 在`<Context>`元素引用全局资源。在你的Web应用程序的上下文配置文件(通常是`META-INF/context.xml`),你可以使用`<ResourceLink>`元素引用全局资源。例如: ```xml <Context> <ResourceLink name="jdbc/myLocalDataSource" global="jdbc/myDataSource" type="javax.sql.DataSource"/> </Context> ``` 在上面的示例,我们将全局资源`jdbc/myDataSource`映射到本地资源`jdbc/myLocalDataSource`。 4. 在你的应用程序使用JNDI查找资源。你可以通过在代码使用JNDI查找资源来获取连接池实例。例如,在Java可以这样做: ```java Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); DataSource dataSource = (DataSource) envContext.lookup("jdbc/myLocalDataSource"); Connection connection = dataSource.getConnection(); ``` 在上面的示例,我们通过JNDI查找资源`jdbc/myLocalDataSource`来获取`javax.sql.DataSource`实例,并从获取数据库连接。 请注意,这只是一个基本的示例,具体的配置和使用方法可能会因你的需求和环境而有所不同。你可以根据你的实际情况进行相应的调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值