初学JSP 。 现在做了一个模拟程序,初步模拟论坛。主要有如下的代码:<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&password=*******";
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='ShowArticleDetail.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();%>
pid是父节点id。
isleaf是判断是不是叶子节点。
其中tree()是一个递归函数, 把子帖全部找出来也累加在str里。输出是这样的结果:1 蚂蚁大战大象 
2 ----大象被打趴下了 
3 --------蚂蚁也不好过 
4 --------瞎说 
5 ------------没有瞎说 
9 --------大象进医院了 
10 ------------护士是蚂蚁 
6 ----怎么可能 
7 --------怎么没有可能 
8 --------可能性是很大的 输出代码: 
<table>
<%= str %>
</table>但是现在每次刷新网页,上面的内容就会多出来一遍, 刷N遍就多出来N遍。。这个是跟着教程写的,可是教程里没有这样的问题。。

解决方案 »

  1.   

    试了下在页面最后把str清空,是可以的。可是每次刷新页面不是重新加载的么? str不也是重新定义重新赋值的么? 非得手动清空? 
      

  2.   

    str 这个变量你是怎么声明的?
      

  3.   

    String str = "";
    private void tree(Connection conn, int id , int level){
    Statement stmt = null;
    ResultSet rs = null;
    String strPre = "";
    for(int i = 0 ; i< level; i++){
    strPre += "----";
    }
    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>" +
       strPre + "<a href = 'ShowArticleDetail.jsp?id=" + rs.getInt("id") + "'>" + rs.getString("title") +"</a>"
       + "</td></tr>";
    if(rs.getInt("isleaf")!=0){
    tree(conn , rs.getInt("id"), level+1);
    }
    }
    }catch省略。
      

  4.   

     呵呵。 只是他之间的有一段视频是还没有公布出来的, 尤其讲servelet那段。很郁闷啊,他后面只是一句话带过。。听不懂。 麻烦哪位详解一下?
      

  5.   

    或者谁有缺失的那段, 给发个链接 。或者发我邮箱, [email protected]  感激。
    比如servelet那段,还有他最后讲structs spring 那些的。