关于checkbox和servlet交互的问题

canying629 2016-12-24 10:41:46
先申明下,以下的全部是自己写,虽然内容较多,但绝不是从别处转来的,希望不要被误会

最近在做一个servlet+jsp+mysql的网站,牵涉到checkbox,总是无法取到数据,只好来求助了,希望各位不吝赐教,先谢谢了!

首先是jsp,简单描述下,就是先在页面查询数据库,然后输出结果,然后checkbox为选中的数据提交到servlet

<%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>  
<%@ page contentType="text/html;charset=utf-8"%>
<head>
</head>
<body>
<script language="javaScript">
function checkAll(name) {
var si = document.getElementsByName('selecteditem');
for(var i=0; i<si.length; i++)
{
si[i].checked = !si[i].checked;
}
}
function clearAll(name) {
var si = document.getElementsByName('selecteditem');
for(var i=0; i<si.length; i++)
{
si[i].checked = false;
}
}
function chaxun() {
document.chaxun.action = "";
document.chaxun.submit();
}
function tijiao() {

document.chaxun.action = "servlet/peidang";
document.chaxun.submit();
}
</script>

<%
String driverName = "com.mysql.jdbc.Driver";
String userName = "user";
String userPasswd = "pwd";
String dbName = "testDB";
String tableName = "data_temp";
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" + userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql = "select mo,item_id,item_name from "
+ tableName
+ " where item_id = '" + request.getParameter("item_id")
+ "'";
System.out.println("sql = " + sql);
ResultSet rs = statement.executeQuery(sql);
%>

<table>
<form name="chaxun" method="post" action="">
<tr align="left" width="400" height="50">
<td><a style="float:left" href="index.jsp" target="_top" onclick="return confirm('确认要返回吗?')">返回首页</a></td>
</tr>
<tr align="left" width="400" height="50">

<td width="250">请输入item_id :  <font color="blue"></td>
<td>
<input type="text" name="item_id" style="width:150px;height:30px">
</td>
<td>
<input type="submit" value=" 确认 " style="width:100px;height:30px" onclick="chaxun()">
</td>
</tr>
</table>
<table>
<thead>
<tr align="center" height="50">
<td>
<span></span>
</td>
<td>
<span>mo</span>
</td>
<td>
<span>item_id</span>
</td>
<td>
<span>item_name</span>
</td>
</tr>
</thead>
<%
while (rs.next()) {
%>
<tbody>
<tr align="center" height="50">
<td width="overflow-x:auto" height="50">
<input type="checkbox" style="width:30px;height:30px" checked="checked" value="" name="selecteditem">
</td>
<td width="overflow-x:auto" height="50" id="mo">
<%
out.print(rs.getString(1));
%>
</td>
<td width="overflow-x:auto" height="50" id="item_id">
<%
out.print(rs.getString(2));
%>
</td>
<td width="overflow-x:auto" height="50" id="item_name">
<%
out.print(rs.getString(3));
%>
</td>
</tr>
</tbody>
<%
}
%>
</table>
<table bgcolor="#FFFFFF" border="0" cellspacing="0">
<tr align="center" width="400" height="50">
<td></td>
</tr>
</table>
<table bgcolor="#FFFFFF" border="0" cellspacing="0">
<tr align="center" width="400" height="50">
<td width="130">
<input type="button" value=" 全选 " style="width:100px;height:30px" onClick="checkAll('selecteditem')" id="qx">
</td>
<td width="130">
<input type="button" value=" 全选解除 " style="width:100px;height:30px" onClick="clearAll('selecteditem')" id="jc"">
</td>
<td width="140">
<input type="button" value=" 提交 " style="width:100px;height:30px" onClick="tijiao()" id="qr">
</td>
</tr>
</form>
</table>
</body>


===============================================================
然后是servlet,通过传来的数据,作为条件,在数据库中做update

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

String[] mo= request.getParameterValues("selecteditem");
String updmo = "";
System.out.println("get mo : " +mo);
Connection conn = null;
String sql;
String url = "jdbc:mysql://localhost:3306/testDB?user=user&password=pwd";
response.setContentType("text/html");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
for(int i= 0 ; i <mo.length ; i++ )

{
updmo+=mo[i]+"','";
System.out.println("updmo:"+updmo);
sql = "update data_temp set status = '1' where mo in ('"
+ updmo + "')";
System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
ps.execute();
}

PrintWriter out = response.getWriter();
out.flush();
out.close();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}


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

doGet(request, response);
}

===========================================================
问题出现了,在servlet中用getParameterValues取到的值都是"on",而不是jsp上mo列的数据,
网上查了很多例子,说是要设置value值,但那都是取的静态的数据,只能取到一个静态的值。
如何能取到查询结果中mo这列的数据呢?诚心求教,希望能有老司机帮一把,再次谢谢了。
...全文
164 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
canying629 2016-12-24
  • 打赏
  • 举报
回复
哦哦,原来是这个意思,之前好像理解错了,谢谢大神的回复!
孟子E章 2016-12-24
  • 打赏
  • 举报
回复
<input type="checkbox" style="width:30px;height:30px" checked="checked" value="<%=rs.getString(1)%>" name="selecteditem"> 你只有赋值了后台才能得到啊

87,904

社区成员

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

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