LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2465|回复: 3

mysql,tomcat连接池配置及jsp测试疑问

[复制链接]
发表于 2005-8-1 02:43:41 | 显示全部楼层 |阅读模式
按照一些资料我先修改了
$CATALINA_HOME/conf/server.xml

找到配置context的地方,添加了如下元素:

<Context path="/DBTest" docBase="E:\eclipse\workspace\DBTest\work"
       debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger"
            prefix="localhost_DBTest_log." suffix=".txt"
            timestamp="true"/>

<Resource name="jdbc/TestDB"
              auth="Container"
              type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TestDB">
   <parameter>
     <name>factory</name>
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
   </parameter>

    <!-- maxActive: Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
         <parameter>
     <name>maxActive</name>
     <value>100</value>
     </parameter>

    <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->
         <parameter>
     <name>maxIdle</name>
     <value>30</value>
   </parameter>

    <!-- maxWait: Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
         <parameter>
     <name>maxWait</name>
     <value>10000</value>
   </parameter>

    <!-- username and password: MySQL dB username and password for dB connections  -->
    <parameter>
    <name>username</name>
    <value>root</value>
   </parameter>
   <parameter>
    <name>password</name>
    <value>16357336</value>
   </parameter>
    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->
         <parameter>
      <name>driverClassName</name>
      <value>com.mysql.jdbc.Driver</value>
   </parameter>
   
    <!-- url: The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
         <parameter>
     <name>url</name>
     <value>jdbc:mysql://localhost:1433;databasename=student</value>
   </parameter>
</ResourceParams>

</Context>





能够正常启动tomcat服务器。

之后我利用eclipse新增一个名为DBTest的tomcat project,编写了web.xml
其内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
   <!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_4.dtd">
<web-app>
<description>MySql DB Test App</description>
<resource-ref>
     <description>DB Connection</description>
     <res-ref-name>jdbc/TestDB</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
</resource-ref>
</web-app>


接着写了一个名字叫test.jsp的文件。文件内容如下:
<%@page contentType="text/html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
        <title>JSP Page</title>\n\
</head>

<body bgcolor="#FFFFFF">

<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>
<%
        InitialContext ctx=new javax.naming.InitialContext();
        DataSource ds=(javax.sql.DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
        Connection conn=ds.getConnection();
        String sql="SELECT * FROM courses";
        Statement stmt = conn.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next()){
                out.print("<tr><td>"+rs.getString(1)+"</td>");
                out.print("<td>"+rs.getString(2)+"</td>");
                out.print("<td>"+rs.getString(3)+"</td>");
                out.print("<td>"+rs.getString(4)+"</td></tr>");

        }
%>

</body>

</html>



用eclipse将tomcat启动,把刚刚写的tomcat project部署。
打开浏览器输入http://localhost:8080/DBTest/test.jsp
出现了http 404 file not found错误。


总结过程:
1,修改了server.xml
2,建立一个名为DBTest的tomcat project
2,新建web.xml文件
4,新建test.jsp文件

问题:
     1,serverl.xml有错误吗?还是web.xml有错误,还是两者都有?
     2,怎么会出现http 404错误的?
 楼主| 发表于 2005-8-1 02:50:19 | 显示全部楼层
图像处的文字应为
冒号:和comp
回复 支持 反对

使用道具 举报

发表于 2005-8-1 03:00:23 | 显示全部楼层
404 :未找到请求资源,

确认访问的资源书写正确
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-8-3 14:01:42 | 显示全部楼层
书写格式没有问题
后来修改jsp测试文件重新测试时
说can't load driver class,
后来就把server.xml改了
改着改着问题就解决了

修改地方有:
1,server.xml
<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="localhost_log." suffix=".txt" timestamp="true" />

<!--因为我换了工作目录,因此 docBase,workDir也改了-->
<Context path="/DataSourceTest" reloadable="true"
docBase="C:\Documents and Settings\ginge\workspace\DataSourceTest"
workDir="C:\Documents and Settings\ginge\workspace\DataSourceTest\work">

<!--将resource 的子元素name,driver class这类全部换成属性,
这是根据tomcat自带的关于JDBC DataSource的Documentation
修改的
-->
<Resource name="jdbc/TestDataSource" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="root" password="16357336"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/student?autoReconnect=true" />


2,修改了测试用的JSP文件:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page
        import="javax.naming.*,java.io.*,javax.sql.*,java.sql.*"%>
<%
                String path = request.getContextPath();
                String basePath = request.getScheme() + "://" + request.getServerName()
                                + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html; charset=GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'dbtest.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>


<h1>test Tomcat</h1>
<%Context initCtx = null;
                DataSource ds = null;
                Context envCtx = null;
                Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;

                try {
                        initCtx = new InitialContext();
                        envCtx = (Context) initCtx.lookup("java:comp/env");

                } catch (Exception x) {
                        x.printStackTrace();
                        if (envCtx == null)
                                System.out.println("envCtx is null");
                }

                try {
                        ds = (DataSource) envCtx.lookup("jdbc/TestDataSource");
                } catch (Exception p) {
                        p.printStackTrace();
                        if (ds == null) {
                                PrintWriter pw = response.getWriter();
                                pw.println("<h2>Can't not create Connection</h2>");
                        }
                }
                System.out.println("before getting connection!");

                try {
                        conn = ds.getConnection();
                        if (conn == null) {
                                PrintWriter pw = response.getWriter();
                                pw.println("<h2>Can't not create Connection</h2>");
                        }
                        out.println("data from database:<br>");
                        stmt = conn.createStatement();
                        rs = stmt.executeQuery("select * from courses");
                } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("before while loop");
                }

                %>
<TABLE>
        <%try {
                        while (rs.next()) {
                                out.print("<tr><td>" + rs.getString(1) + "</td>");
                                out.print("<td>" + rs.getString(2) + "</td>");
                                out.print("<td>" + rs.getString(3) + "</td>");
                                out.print("<td>" + rs.getString(4) + "</td></tr>");

                        }
                } catch (Exception b) {
                        b.printStackTrace();
                        System.out.println("in while loop");
                }

                %>
        <%try {
                        rs.close();
                        stmt.close();
                } catch (Exception c) {
                        c.printStackTrace();
                        System.out.println("try to close statement");
                        rs.close();
                        stmt.close();
                } finally {
                }

        %>
        <
</body>
</html>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表