日期:2014-05-17  浏览次数:20652 次

JAVA新手,form表单提交输入的数据,JSP显示重复的记录




在重新输入数据,提交数据以后,原来的一条重复记录被覆盖了

servlet DOPOST 代码如下

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

messageBoardDAO msg=new messageBoardDAO();
String addresser=((String)request.getParameter("addresser"));
String content=((String)request.getParameter("content"));
  try {
msg.insertMsgBoard(addresser, content);
} catch (Exception e) {
e.printStackTrace();
}
List msgList=new ArrayList();
msgList.add(msg.selectMsgBoard(addresser, content));
for(int i=0;i<msgList.size();i++){
messageBoard msgBoard=(messageBoard)msgList.get(i);
request.setAttribute("message1",msgBoard.getAddresser());
System.out.println(msgBoard.getAddresser());
request.setAttribute("message2",msgBoard.getContent());
System.out.println(msgBoard.getContent());
}
 
request.getRequestDispatcher("/messageBoard.jsp").forward(request, response);
}


JSP页面部分代码
<tr>
  <td >
  发言人:${message1} <br>
  发言内容:${message2}<br>
  </td>
  </tr>
  </table>
  <hr>
  <table>
  <tr>
  <td>
  <form action="messageBoardServlet" method="post" >
  发言人:<input type="text" name="addresser" value="" size=20><br>
  <textarea name="content" rows="7" cols="60"></textarea><br>
  <input type="submit" name="submit" value="提交">
  </form>
  </td>
  </tr>




------解决方案--------------------
Java code

msgList.add(msg.selectMsgBoard(addresser, content));