问题描述
- 用window.location.href为什么跳转不到servlet里??
-
删除
function empDelete(){var obj = document.getElementsByName("del_flag"); var arr = new Array (); var obj = document.getElementsByName("del_flag"); for (var i = 0;i<obj.length;i++) { if (obj[i].checked) { arr.push(obj[i].value); } } //alert(arr); if (arr.length == 0) { alert("请选择要删除的记录!"); } else { window.location.href = "EmployeeDeleteServlet?cb="+arr; alert(arr); } }
servlet里:
String[] df= request.getParameterValues("cb");for(int j = 0;j<df.length;j++){ System.out.println(df[j]); } Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; int result = 0; try { conn = DBUtil.getConn("employee"); if(df != null && df.length>0){ for(int i = 0;i<df.length;i++){ pst = DBUtil.getPst(conn,"delete from EmpInfo where num = ?"); pst.setString(1, df[i]); result = pst.executeUpdate(); } if(result > 0){ response.sendRedirect("employee_list.jsp"); }else{ request.setAttribute("errmsg", "删除数据失败"); request.getRequestDispatcher("error.jsp").forward(request, response); } } } catch (SQLException e) { e.printStackTrace(); } }
解决方案
在跳转地址EmployeeDeleteServlet前面加上./ 这样 ./EmployeeDeleteServlet?cb
解决方案二:
window.location window.location.href 跳转url区别
解决方案三:
window.location.href = "EmployeeDeleteServlet?cb="+arr;
在这上面打出一个alert语句。输出"EmployeeDeleteServlet?cb="+arr这一段,看看是不是地址错了
时间: 2024-08-25 06:13:07