Tomcat 配置JNDI数据源问题
我自JSP页面里通过JNDI读取tomcat的服务器配置,但是运行报错如下:
HTTP Status 500 - An exception occurred processing JSP page /First.jsp at line 44
type Exception report
message An exception occurred processing JSP page /First.jsp at line 44
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /First.jsp at line 44
41: Context ctx = new InitialContext();
42: DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/testdb");
43: if (ds == null) return;
44: Connection conn = ds.getConnection();
45: Statement stmt = conn.createStatement();
46: ResultSet rs = stmt.executeQuery("select * from news_inf");
47: while (rs.next()) {
我的tomcat服务器conf目录下context.xml配置如下:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource
name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javaee"
username="root"
password="123456"
maxActive="5"
maxIdle="2"
maxWait="10000"/>
</Context>
项目中WEB-INF目录下web.xml配置如下:
<?xml version="1.0" encoding="gbk"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MySQLJSP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file