请问重新载入jsp页面的问题

guanglin2320 2009-03-01 01:29:58
遇到的问题:在MyEclipse中,首次启动Tomcat服务器,第一次执行可以正确显示页面结果,而当点击刷新,或在浏览器重复载入,或在MyEclipse重复Run时,jsp页面上显示的结果就是在原来的基础上又增加了一倍,而不是重复执行一次
只有将Tomcat关闭再打开,第一次执行的才是正确的,否则,每次执行载入,jsp页面上的结果都会递增+1

如果我说得不够明白,大家可以把源代码复制运行一下,期待各位的帮助.
源代码如下:

-------------------------------------------------------------------------------

<%@ 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 stm = null;
ResultSet rs = null;
String preStr = "";
for(int i = 0;i<level;i++){
preStr += "----";
}
try{
stm = conn.createStatement();
rs = stm.executeQuery("select * from article where pid =" + id);
while(rs.next()){
str += "<tr><td>" + rs.getInt("id") + "</td><td>"
+ preStr + rs.getString("title") + "</td></tr>";

if(rs.getInt("ifleaf") != 0){
tree(conn,rs.getInt("id"),level+1);
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(rs!=null)try{rs.close();rs=null;}catch(Exception e){}
if(stm!=null)try{stm.close();stm=null;}catch(Exception e){}
}
}
%>
<%
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=root");
stm = conn.createStatement();
rs = stm.executeQuery("select * from article where pid = 0");
while(rs.next()){
str += "<tr><td>" + rs.getInt("id") + "</td><td>"
+ rs.getString("title") + "</td></tr>";
if(rs.getInt("ifleaf")!=0){
tree(conn,rs.getInt("id"),1);
}
}

rs.close();
stm.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 bgcolor="green">
<table border="1" cellspacing="0">
<%= str %>
</table>
</body>
</html>



-------------------------------------------------------------------------------
...全文
362 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaHtmlCssJSP 2009-03-01
  • 打赏
  • 举报
回复
也不是毫无用处的啊……,比如我就学习到了~
  • 打赏
  • 举报
回复
这个帖子楼主可以不满意结账,分值会返回给你的

  • 打赏
  • 举报
回复
[Quote=引用 1 楼 guanglin2320 的回复:]
诶,没人理我,不过问题找到了

把成员变量str设为null就可以了

<% str=null;%>
[/Quote]
不好意思,来晚了

其实楼主没有必要灰心
可能是发帖的这段时间大家都没在线

鼓励楼主

加油
guanglin2320 2009-03-01
  • 打赏
  • 举报
回复
诶,没人理我,不过问题找到了

把成员变量str设为null就可以了

<% str=null;%>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧