一个jsp页面
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*"%><%!    String str = ""; private void tree(Connection conn, int id, int level) {
Statement stmt = null;
ResultSet rs = null;
String prestr = "";
for (int i = 0; i < level; i++) {
prestr += "----";
}
try {
stmt = conn.createStatement();
String sql = "select * from article where pid =" + id;
rs = stmt.executeQuery(sql);
while (rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" + prestr
+ "<a href = 'showdetail.jsp?id=" + rs.getInt("id")
+ "'>" + rs.getString("title") + "</a>" + "</td></tr>";
if (rs.getInt("isleaf") != 0) {
tree(conn, rs.getInt("id"), level + 1);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}%><%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=root";
Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("Select * from article where pid = 0"); while (rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>"
+ "<a href = 'showdetile.jsp?id=" + rs.getInt("id")
+ "'>" + rs.getString("title") + "</a>" + "</td></tr>";
if (rs.getInt("isleaf") != 0) {
tree(conn, rs.getInt("id"), 1);
}
}
rs.close();
stmt.close();
conn.close();
%><!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=gbk">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%=str%>
</table>
    <%str = ""; %>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我的问题是:之前我的程序在最后的地方没加<%str = ""; %>  程序每次读取数据库中的10条记录,但是每次我在页面上刷新一次,页面重新读取数据库,在上次显示的10条记录后面重复显示那10条记录,在刷新一次,又接着上次的记录后面重复显示10条,变成了30条记录,后来我在最后加上了<%str = ""; %>,不会出现上面的问题了,但是我想想问问,这样对程序有什么影响,道理是什么?我这样理解对不对:str是一个全局变量,把数据库中的记录全输入到str中,关闭数据库的连接之后用<%=str%>输出,之后给str用 <%str = ""; %>,这个问题是解决了,但是因为str已经被清空,但是输出到浏览器的每条记录都是一个连接,这样会对这些连接或者说以后的操作有什么影响?小弟是新人,好多问题不明白,请大侠指点!谢谢了先!

解决方案 »

  1.   

    就如下面:<%!
         int i = 0;
    %>
    <body>
        <%=i++%>
    </body>这样你每次刷新i都会增1,道理和你的str一样。。
      

  2.   

    你的基础知识确实需要加强。<%! 这个是全局声明,<%! String str="" %>等于定义了一个全局变量str,所有的页面都可以用到它并改变它。你每打开一次页面,str的值就发生一次变化,所以你开始每刷新一次,str就多10条。你加上<% str="" %>等于把str的值改变并使用了以后,再把它的值清空了。至于你说的影响是没有的。只是不明白你定义全局变量的用意。你可以把<%! String str="" %> 换成 <% String str="" %>,这样就不需要 <% str="" %>了。
      

  3.   

    楼上的,我换成了<% String str="" ;%> 但是无法被<%!%>中的private void tree(Connection conn, int id, int level) str引用
      

  4.   

    楼上的,我换成了 <% String str="" ;%> 但是无法被 <%!%>中的private void tree(Connection conn, int id, int level) str引用,这样写在str += "<tr><td>" + rs.getInt("id") + "</td><td>" + prestr
    这个地方报错啊
      

  5.   

    jsp页面不要特殊对待它 实际上他就是个普通的java程序。。所以全局的概念仍然存在。。
      

  6.   

    我开始说过了,不明白你用全局声明的意义。你可以先说下你的意图。
    你把所有<%!%>包含的东西都换成<%%>就可以了。当然这是在没考虑你的意图的情况下,去掉<%str=""%>的途径之一。
      

  7.   

    要定义成全局变量。不过在下次刷新时候要清空下。。
    str = “”;
      

  8.   

    楼主,你代码逻辑有问题吧,怎么自身访问自身?
    <%!      String str = ""; private void tree(Connection conn, int id, int level) {
    Statement stmt = null;
    ResultSet rs = null;
    String prestr = "";
    for (int i = 0; i < level; i++) {
    prestr += "----";
    }
    try {
    stmt = conn.createStatement();
    String sql = "select * from article where pid =" + id;
    rs = stmt.executeQuery(sql);
    while (rs.next()) {
    str += " <tr> <td>" + rs.getInt("id") + " </td> <td>" + prestr
    + " <a href = 'showdetail.jsp?id=" + rs.getInt("id")
    + "'>" + rs.getString("title") + " </a>"
    + " </td> </tr>";
    if (rs.getInt("isleaf") != 0) {
    tree(conn, rs.getInt("id"), level + 1); }
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    try {
    if (rs != null) {
    rs.close();
    rs = null;
    }
    if (stmt != null) {
    stmt.close();
    stmt = null;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    %>