我有一个页面需要学生出勤。它打印出一个学生列表,每个学生旁边都有一个复选框。然后,该人单击提交,然后将所选学生的ID和实验室ID发送到数据库。
我遇到的问题是它会将最后一个被打勾的框发送到数据库,其余的都不发送。我假设它会覆盖之前的内容,但不确定如何解决此问题。
这是我的doPost方法:
doPost
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int user_id = Integer.parseInt((String)request.getParameter("user_id")); int lab_id = Integer.parseInt((String)request.getParameter("lab_id")); System.out.println("I got a blow job"); String message = null; try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); System.out.println("got connection"); Statement s = con.createStatement(); String sql = "INSERT INTO user_lab" + " (user_id, lab_id)" + " VALUES" + " ('" + user_id + "'," + " '" + lab_id + "')"; System.out.println(sql); int i = s.executeUpdate(sql); if (i==1) { message = "Successful attendance."; response.sendRedirect("Tutor_labs"); } s.close(); con.close(); } catch (SQLException e) { message = "Error." + e.toString(); boolean error = true; } catch (Exception e) { message = "Error." + e.toString(); boolean error = true; } if (message!=null) { PrintWriter out = response.getWriter(); out.println("<B>" + message + "</B><BR>"); out.println("<HR><BR>"); } } }
这是我的JSP页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Mars University Lab System</title> <link rel="stylesheet" href="style.css" type="text/css" media="screen"> </head> <body> <jsp:include page="headerTutor.jsp"/> <div id = "centrecontent"> <h3>Students In Tutorial</h3> <h2>Attendance List</h2> <% String[] list1 = (String[])request.getAttribute("res1"); String[] list2 = (String[])request.getAttribute("res2"); String[] list3 = (String[])request.getAttribute("res3"); String[] list4 = (String[])request.getAttribute("res4"); if(null == list1){%> <th>Uni Id</th><th>Name</th><th>Email</th> <% }else{ %> <form name="ViewStudentsTutor" ACTION="http://www.crackman.net.au/ICE/test.php\" method="post"> <% for(int i=0; i<list1.length; i++) { %> <input type="hidden" name="lab_id" value=<%=request.getParameter("labid")%>> <%out.println(list2[i]);%> <%out.println(list3[i]);%> <%out.println(list4[i]); %> <input type="checkbox" name="user_id" value=<%out.println(list1[i]);%>><br> <% }} %> <input type="submit" value="Submit" name="Submit"/> </form> </div> <jsp:include page="footer.jsp"/> </body> </html>
您只user_id使用一个,request.getParameter("user_id") 而是应该使用request.getParameterValues("user_id"),它将返回您想要String的所有user_ids 的数组,而不是user_id返回当前获得的任何一个
user_id
request.getParameter("user_id")
request.getParameterValues("user_id")
String
类似的是 lab_id
lab_id