Tomcat5.5部署应用及配置数据源

测试环境tomcat5.5,oracle10g
一部署:
在%Tomcat_HOME%conf\server.xml中,找到<GlobalNamingResources> </GlobalNamingResources>,在其节点添加如下:
<Context path="/haha" docBase="D:\myproject\TestCatDS\WebRoot" debug="0" reloadable="true"/>
注:
    contex指上下文,实际上就是一个web项目;
    path是虚拟目录,访问的时候用127.0.0.1:8080/haha/index.jsp访问网页,haha前面要加/;
    docBase是网页实际存放位置的根目录,映射为path虚拟目录;
     reloadable="true"表示你修改了jsp文件后不需要重启就可以实现显示的同步。
配置完毕后,启动tomcat,如果访问http://localhost:8080/haha,能够正常显示index.jsp页面,则说明部署成功。
二配置数据源:
1.将classes12.jar考到%Tomcat_HOME%common\lib下
2.在%Tomcat_HOME%conf\server.xml中的找到 <GlobalNamingResources> </GlobalNamingResources>,在此节点添加:
<Resource name="jdbc/tomcat" 
              auth="Container" 
              type="javax.sql.DataSource" 
              maxActive="100" 
              maxIdle="30" 
                      maxWait="10000" 
                      username="gg" 
                      password="123456" 
                      driverClassName="oracle.jdbc.driver.OracleDriver" 
                      url="jdbc:oracle:thin:@localhost:1521:mydb"/> 
添加完毕后<GlobalNamingResources>节点的内容如下:
<GlobalNamingResources>

    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>

    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />

     <Resource name="jdbc/tomcat" 
              auth="Container" 
              type="javax.sql.DataSource" 
              maxActive="100" 
              maxIdle="30" 
                      maxWait="10000" 
                      username="gg" 
                      password="123456" 
                      driverClassName="oracle.jdbc.driver.OracleDriver" 
                      url="jdbc:oracle:thin:@localhost:1521:mydb"/> 
  </GlobalNamingResources>
3.修改%Tomcat_HOME%conf\context.xml,在<Context></Context>节点中添加如下代码:
<Resource name="jdbc/tomcat" 
              auth="Container" 
              type="javax.sql.DataSource" 
              maxActive="100" 
              maxIdle="30" 
                      maxWait="10000" 
                      username="gg" 
                      password="12356" 
                      driverClassName="oracle.jdbc.driver.OracleDriver" 
                      url="jdbc:oracle:thin:@localhost:1521:mydb"/> 
添加完毕后context.xml具体内容为:
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
<Resource name="jdbc/tomcat" 
              auth="Container" 
              type="javax.sql.DataSource" 
              maxActive="100" 
              maxIdle="30" 
                      maxWait="10000" 
                      username="gg" 
                      password="123456" 
                      driverClassName="oracle.jdbc.driver.OracleDriver" 
                      url="jdbc:oracle:thin:@localhost:1521:mydb"/> 

</Context>
4.修改应用目录下的web.xml文件。在<web-app></web-app>节点出添加以下代码:
<resource-ref>
   <description>dhcc</description>
   <res-ref-name>jdbc/tomcat</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
添加完毕后web.xml的具体内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>ipsdb</description>
<res-ref-name>jdbc/tomcat</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>
5.测试代码如下:
根据数据源名称获得Connection的方法:
/**
* Tomcat 取得数据库连接 输入datasource
*
* @return Connection
*/
public Connection getTomcatConnection(String datasource) {
Connection conn = null;// 连接对象
DataSource ds = null;// 数据源对象

try {
Context ctx = new InitialContext();// 初始化上下文对象
Context envCtx = (Context) ctx.lookup("java:comp/env");// 查找数据源
ds = (DataSource) envCtx.lookup(datasource);
conn = ds.getConnection();// 获取连接
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
logger.error("取得连接查找失败:", e);
}

return conn;
}
调用此方法的的jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.dhcc.util.ServiceLocater"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%
Connection con = ServiceLocater.getInstance().getTomcatConnection(
"jdbc/tomcat");
Statement st = null;
ResultSet rs = null;
String sql = "select * from test";
try {
st = con.createStatement();
rs = null;
rs = st.executeQuery(sql);
out.println(rs == null);
out.println("<br>");
if (rs != null) {
while (rs.next()) {
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println("<br>");
}
}

} catch (SQLException e) {
System.out.println("查询数据失败!error: " + e.getMessage());
} finally {
rs.close();
st.close();
con.close();
}
%>

你可能感兴趣的:(sql,tomcat,Web,jsp,SQL Server)