Tomcat下配置JNDI

tomcat: 7.0
jdbc: mysql-connector-java-5.1.18

把 mysql-connector-java-5.1.18-bin.jar 拷贝到“$CATALINA_HOME/lib”

第一步,配置资源
在$CATALINA_HOME/conf/server.xml中,找到Engine->Host,在Host下面增加
<Context path="/testwebapp" docBase="D:/workspace/testwebapp/WebContent" debug="0" reloadable="true">
<Resource name="jdbc/MySQLDB" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxActive="10" maxIdle="5" maxWait="10000"
username="testwebapp" password="testwebapp"
url="jdbc:mysql://localhost:3306/testwebapp"
description="MySQLDB for testwebapp Applications" />
</Context>

或者使用全局资源配置,在$CATALINA_HOME/conf/server.xml的<GlobalNamingResources>中增加全局资源配置
<Resource name="jdbc/MySQLDB" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxActive="10" maxIdle="5" maxWait="10000"
username="testwebapp" password="testwebapp"
url="jdbc:mysql://localhost:3306/testwebapp"
description="MySQLDB for testwebapp Applications" />
在Context中,使用ResourceLink连接全局资源
<Context path="/testwebapp" docBase="D:/workspace/testwebapp/WebContent" debug="0" reloadable="true">
<ResourceLink name="jdbc/MySQLDB" global="jdbc/MySQLDB" type="javax.sql.DataSource" />
</Context>

第二步,在testwebapp/WebContent/WEB-INF/web.xml中</display-name>增加下面内容:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MySQLDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

第三步,创建测试jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;

String sql=null;
String username=null;
String alias=null;
String pwd=null;
String strMsg=null;
try{
Context cxt = new InitialContext();
DataSource ds = (DataSource)cxt.lookup("java:/comp/env/jdbc/MySQLDB");
conn = ds.getConnection();
sql="SELECT 1 ";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery(sql);
if( rs.next() ){
strMsg="连接ok";

out.println("连接ok");
}else{
out.println("rs.next() fail");
}
rs.close();

} catch(Exception e){
out.println("连接失败:"+e.getMessage());
//e.printStackTrace();
}

try{
if(stmt!=null) stmt.close();
}catch(Exception e){}

try{
if(conn!=null) conn.close();
}catch(Exception e){}


%>
</body>
</html>

注意:如果使用“MySQL Connector/J”文档中“Using Connector/J with Tomcat”配置内容,如下:
<Resource name="jdbc/MySQLDB" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/MySQLDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter><parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>SELECT 1</value>
</parameter>
<parameter>
<name>testOnBorrow</name>
<value>true</value>
</parameter>
<parameter>
<name>testWhileIdle</name>
<value>true</value>
</parameter>
<parameter>
<name>timeBetweenEvictionRunsMillis</name>
<value>10000</value>
</parameter>
<parameter>
<name>minEvictableIdleTimeMillis</name>
<value>60000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/testwebapp</value>
</parameter>
<parameter>
<name>username</name>
<value>testwebapp</value>
</parameter>
<parameter>
<name>password</name>
<value>testwebapp</value>
</parameter>
</ResourceParams>
会出现错误: Cannot create JDBC driver of class '' for connect URL 'null'
因此下面的内容需要作为<Resource>的属性来配置
driverClassName="com.mysql.jdbc.Driver"
maxActive="10" maxIdle="5" maxWait="10000"
username="testwebapp" password="testwebapp"
url="jdbc:mysql://localhost:3306/testwebapp"

如果没有在$CATALINA_HOME/conf/server.xml中配置testwebapp的Context,则可以在testwebapp/META-INF/context.xml中定义连接池,这样适合直接把testwebapp拷贝到$CATALINA_HOME/webapps目录的情况。
如果在$CATALINA_HOME/conf/server.xml中配置了testwebapp的Context,那么以server.xml的配置为准,testwebapp/META-INF/context.xml配置不起作用。
<?xml version='1.0' encoding='utf-8'?>
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- mysql连接池资源定义 -->
<Resource name="jdbc/MySQLDB" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxActive="10" maxIdle="5" maxWait="10000"
username="testwebapp" password="testwebapp"
url="jdbc:mysql://localhost:3306/testwebapp"
description="MySQLDB for testwebapp Applications" />
</Context>
在testwebapp/WEB-INF/web.xml中也需要配置<resource-ref>

ps:1.第二步可以不要

2.webroot下写jsp测试,在.java文件中测试是会报错的,具体原因与tomcat容器有关,因为,tomcat编译的是webroot下面的class文件和jsp 、html ,对于java文件自然是无法获取到tomcat 容器中的JNDI了


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值