开源技术之Tomcat数据源配置总结

开发者在线 Builder.com.cn 更新时间:2007-09-24作者:中国IT实验室 来源:中国IT实验室

四、    如果你在Eclipse或JBuilder中开发的话,你需要在你的Web应用程序的WEB-INFWeb.xml文件中注册数据源,文件添加如下内容:
    <resource-ref>
        <res-ref-name>jdbc/northwind</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
一定注意:同时检查一下你部署到Tomcat中对应的
彩色的加粗文字是添加上的,用来注册数据源的JNDI,在这我注册了两个数据源,一个是oracle的,一个是MSSQL  Server 2000的。

在做任何配置时最好不要修改Tomcat服务器的任何文件,如servel.xml或web.xml文件,而所有的操作和配置都可以在你自己的应用配置文件中来完成,这样即使培植错误也不至于服务器的崩溃。

按以上步骤就可以完成数据源的配置,你可以写一些程序来测试。
用JSP来测试,Index.jsp文件程序如下:

<%@ page language="java" import="java.util.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.println(basePath);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br> 

    <%         
 Context ctx=null;
   Connection cnn=null;
   java.sql.Statement stmt=null;
   ResultSet rs=null;
   try
   catch(Exception e)

   }
   finally
   

   
    %>
  </body>
</html>
在你的浏览器中运行http://10.0.0.168:8888/WebDemo/web/即可以看到结果:如下:
 
 
你看到连接成功的标志,就意味这你的数据源配置成功!!!

记住:要想配置成功,就要认真检查需要配置的每一个细节。

查看本文来源

用户评论

  • 用户名
  • 评论内容