网页中显示Name [jdbc/webdb] is not bound in this Context. Unable to find [jdbc].
以下是我的ViewDictionary.java
try
{
out.println("<b>Hello world!</b>");
//获得Context对象实例
javax.naming.Context ctx = new javax.naming.InitialContext();
//根据webdb数据源获得DataSource对象
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/webdb");
out.println("<b>Hello world!</b>");
//获得Connection对象
Connection conn = ds.getConnection();
}
project名字是webdemo
WebRoot-WEBINF-web.xml中配置如下:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ViewDictionary</servlet-name>
<servlet-class>chapter4.ViewDictionary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewDictionary</servlet-name>
<url-pattern>/servlet/viewdictionary</url-pattern>
</servlet-mapping>
tomcat安装目录下conf文件夹中sever.xml中相关配置如下:
<GlobalNamingResources>
<Resource name="jdbc/webdb" auth="Container"
type="javax.sql.Datasource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3300/webdb"
username="root"
password="steacy911"
maxActive="200"
maxIdle="50"
maxWait="3000"/>
</GlobalNamingResources>
tomcat安装目录下\conf\Catalina\localhost\webdemo.xml如下:
<Context path="/webdemo" docBase="webdemo" debug="0">
<ResourceLink name="jdbc/webdb" global="jdbc/webdb" type="javax.sql.DataSource" />
</Context>
mysql中实例的port为3300.