真是莫名其妙,谁能帮我解释一下是什么问题?
一个简单的留言本,是用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>