首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

javaweb动态生成投票选项的有关问题

2012-05-31 
javaweb动态生成投票选项的问题index.jsp:%@ page contentTypetext/html charsetUTF-8 languageja

javaweb动态生成投票选项的问题
index.jsp:
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态生成投票选项</title>
<script language="javascript" type="">
  function Mycheck(){
  if(form2.name.value=="")
  { 
  alert("请输入投票信息!");
  return false;
  }
  form2.submit();
  }
</script>
</head>
<body>
<table width="570" height="275" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  <td align="right" background="bg.gif">
  <table width="80%" height="104" border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td width="10%">&nbsp;</td>
  <td width="79%" valign="top">
  <%if(session.getAttribute("list")!=null){%>
  <table width="315" border="0" cellpadding="0" cellspacing="0">
  <form name="form1" method="post" action="dealwith.jsp?action=insertAll">  
  <tr>
  <td height="25" colspan="2"><b>您最喜爱的图书评选:</b></td>
  </tr>
  <%
  List list=(List)session.getAttribute("list");
  String allName="";
  for(int i=0;i<list.size();i++){
  allName=(String)list.get(i);
  %>
  <tr>
  <td width="230" height="25">选项名称: </td>
  <td width="85" height="25">
  <input type="text" name="allName" value="<%=allName%>">
  </td>
  </tr>
  <%}%>
  <tr>
  <td height="25" colspan="2"><input type="submit" name="Submit" value="保存投票信息"></td>
  </tr> </form>
  </table>
  <%}%>
<table width="315" border="0" cellpadding="0" cellspacing="0">
  <form name="form2" method="post" action="dealwith.jsp?action=insertOne" onsubmit="return Mycheck()">
  <tr>
  <td height="25"><b>请输入投票选项,并单击【添加投票选项】按钮!</b></td>
  </tr>
  <tr>
  <td height="25"><input type="text" name="name"></td>
  </tr>
  <tr>
  <td height="25"> <input type="submit" name="Submit2" value="添加投票选项" >
  </td>
  </tr>
  </form>
</table>
</td>
  <td width="11%">&nbsp;</td>
  </tr>
</table>
<p>&nbsp;</p></td>
  </tr>


</table>
</body>
</html>

dealwith.jsp:
<%@page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage=""%>
<%@page import="java.util.*"%>
<jsp:useBean id="connection" scope="request" class="com.JDBConnection"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
  request.setCharacterEncoding("UTF-8");
  String action = request.getParameter("action");
  if (action.equals("insertAll")) {
  List list = (List) session.getAttribute("list");
  String allName = "";
  for(int i=0;i<list.size();i++){
  allName=(String)list.get(i);
  String sql="insert into tb_book values ('"+allName+"')";
  connection.executeUpdate(sql);
  }
  connection.closeConnection();
  session.invalidate();
%>
<script language="javascript" type="text/javascript">
alert("添加成功");parent.location.href='index.jsp';
</script>
<% }
  if (action.equals("insertOne")) {
  List list = null;
  if (session.getAttribute("list") == null) {
  list = new ArrayList();
  }else {
  list = (List) session.getAttribute("list");
  }
  list.add(request.getParameter("name"));
  session.setAttribute("list", list);
  response.sendRedirect("index.jsp");
  }
%>

问题:运行是没有问题的,点击”添加选票选项“后,原界面既然多了两条内容·是相同的··比如输入“111”,界面里
多出两个文本框里写着“111”··应该是多出一个文本框啊= =表示鸭梨很大··跪求达人!!!

[解决办法]
你为什么提交的时候又取了一遍list,把list的值都插入数据库了?
插入最新输入的不就可以了?

热点排行