1. Tomcat-5.5.17 环境配置
1.1 下载 Admin 模块, 把压缩包里面的 conf 和 server 文件夹直接拷贝到 Tomcat 根目录下.
1.2 增加 Tomcat 系统管理员角色和用户.在 conf/tomcat-users.xml 文件中增加如下内容:
<role rolename="admin"/>
<user username="admin" password="admin" roles="manager,admin"/>
1.2 设置 JNDI 数据源
1.2.1 登录 http://url:8080/admin 地址, 进入 Tomcat Web Administrator 界面
1.2.2 进入 Resources --- Data Sources 界面, 新增一个 JNDI 数据源. 如下:
name="jdbc/newbee"
type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
password="esoon"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:jtds:sqlserver://localhost/Hibernate"
注意: 手工添加文件 conf/Catalina/localhost/newbee.xml ,文件名称与应用项目要一致, 内容如下:
(缺少以下内容会导致错误:org.apache.tomcat.dbcp.dbcp.SQLNestedException)
<Context docBase="${catalina.home}/webapps/newbee" privileged="true">
<ResourceLink name="jdbc/newbee" global="jdbc/newbee" type="javax.sql.DataSource"/>
</Context>
在 %tomcat-home%/webapps/newbee/WEB-INF/web.xml 文件中增加如下内容:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/newbee</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
1.2.3、把数据库驱动拷贝到 %Tomcat_home%/common/lib 目录下。
1.3 设置用户和角色
1.3.1 登录 http://url:8080/admin 地址, 进入 Tomcat Web Administrator 界面
1.3.2 在 User Definition 里面定义角色, 分组和用户
2. 注意事项
Tomcat 5.5.12 WARNING: A docBase XXX inside the host appBase has been specified, and will be ignored
原因:
在 conf/server.xml 文件中有以下内容
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps" ## 指定了应用的默认路径是 ${catalina.home}/wabapps 目录
name="localhost">
</Host>
</Engine>
在 conf/catalina/localhost/xxx.xml (xxx是你的项目名称)用以下内容
<Context path="/newbee" docBase="${catalina.home}/webapps/newbee" privileged="true">
<ResourceLink name="jdbc/newbee" global="jdbc/newbee" type="javax.sql.DataSource"/>
</Context>
其中 docBase 指定的内容与上面 appBase 指定的路径重复,因此忽略后者的指定。如果要消除警告信息,只要去掉 docBase
属性即可。docBase 只用于和 appBase 指定的默认路径不一致的情况。
3、Tomcat_5.5.17全局JNDI数据源配置
3.1、在 /conf/context.xml 中添加如下内容
<Resource
name="jdbc/njs"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:qadb"
validationQuery="select * from dual"
username="qa"
password="qa"
maxWait="50"
maxIdle="4"
maxActive="4" />
3.2、配置每个应用的 web.xml 文件
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/njs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.3、在jsp中测试JNDI数据源
<!-- JNDI TEST -->
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.*"%>
<%
Connection connection = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/zlfx");
connection = ds.getConnection();
PreparedStatement ps = connection.prepareStatement("select * from tb_gqa_ad");
ResultSet it = ps.executeQuery();
while (it.next()) {
out.write(it.getObject(2).toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (connection!=null) {
connection.close();
connection = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
%>