真是莫名其妙,谁能帮我解释一下是什么问题?

halenabc 2005-08-22 05:31:07
一个简单的留言本,是用application存数据的,但是不知道是什么问题,最开始的一条记录总是被第二条覆盖,比如刚重启完服务器,输入第一条留言,查看留言显示正常;但是输入完第二条再点查看就只有第二条刚才那条就被覆盖了,再输入就都能按顺序正常显示了
请大家帮忙分析一下。
---------------留言页面 submit.jsp-------------------------
<form name="form1" method="post" action="messagePane.jsp">
<p>输入姓名:
<input type="text" name="name">
</p>
<p>输入标题:
<input type="text" name="title">
</p>
<p>输入留言:</p>
<p>
<textarea name="text"></textarea>
</p>
<p>
<input type="submit" name="submit" value="提交">
</p>
</form>
<form name="form2" method="post" action="showMessage.jsp">
<input type="submit" name="look" value="察看留言">
</form>
----------用application保存数据页面 messagePane.jsp -----
<body>
<%!
Vector v=new Vector();
int i=0;
ServletContext application;
synchronized void sendMessage(String s)
{
application=getServletContext();
i++;
System.out.print(i);
v.add("NO."+i+","+s);
application.setAttribute("Mess",v);
}
%>
<%
String name=request.getParameter("name");
String title=request.getParameter("title");
String message=request.getParameter("text");
if(name==null)
{ name="guest"+(int)(Math.random()*10000); }
if(title==null)
{ title="无标题"; }
if(message==null)
{ message="无消息"; }
String s="Name:"+name+"#"+"Title:"+title+"#"+"Content:"+"<br>"+message;
sendMessage(s);out.print("您的信息已经提交");
%>
<a href="submit.jsp">返回</a>
</body>

------------------------显示留言页面---------------------
<body>
<%
Vector v=(Vector)application.getAttribute("Mess");
for(int i=0;i<v.size();i++)
{ String message=(String)v.elementAt(i);
StringTokenizer fenxi=new StringTokenizer(message,"#");
while(fenxi.hasMoreTokens())
{ String str=fenxi.nextToken();
byte a[]=str.getBytes("ISO8859-1");
str=new String(a);
out.print("<br>"+str);
}
}
%>
</body>
...全文
203 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
strawren 2005-08-22
  • 打赏
  • 举报
回复
//不错
//你可以试试
application.setAttribute("Mess",v);
synchronized void sendMessage(String s)
{

application=getServletContext();
i++;
Vecto v1=(Vector)applicaton.getAttribute("Mess")
System.out.print(i);
v1.add("NO."+i+","+s);
application.setAttribute("Mess",v1);
}
amorcupt 2005-08-22
  • 打赏
  • 举报
回复
支持楼上
bluelily22 2005-08-22
  • 打赏
  • 举报
回复
改成这样,你再试试
application.setAttribute("Mess",v);
synchronized void sendMessage(String s)
{

application=getServletContext();
i++;
Vecto v1=(Vector)applicaton.getAttribute("Mess")
System.out.print(i);
v1.add("NO."+i+","+s);
application.setAttribute("Mess",v1);
}
chinatelly 2005-08-22
  • 打赏
  • 举报
回复
你在保存前应该把已经保存的内容读出来,放入到Vector中,再添加新的内容到Vector中,再保存,这样就不会覆盖了
huangdeji 2005-08-22
  • 打赏
  • 举报
回复
眼睛花了
zhangyuyue 2005-08-22
  • 打赏
  • 举报
回复
我跑了一遍,没发现你说的现象。你自己在试试。

81,091

社区成员

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

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