后台servlet设置
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method=request.getParameter("method");
String id=request.getParameter("u_id");
System.out.println("id="+id);
//int u_id=Integer.valueOf(id);
if("displayuser".equals(method)){
List<User> usermessage=userservice.displayUser();
request.setAttribute("list_usermessage", usermessage);
request.getRequestDispatcher("usermessage.jsp").forward(request, response);
}
if("deleteuser".equals(method)){
boolean flag=userservice.deleteUser(Integer.valueOf(id));
if(flag){
List<User> usermessage=userservice.displayUser();
request.setAttribute("list_usermessage", usermessage);
request.getRequestDispatcher("usermessage.jsp").forward(request, response);
}else{
response.getWriter().println("<script>alert(\"删除失败!\");</script>") ;
}
}
}
jsp页面设置
方式一:表单提交(隐藏表单域技术)
<form action="UserMesgServlet?method=deleteuser" method="post">
<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序号</td>
<td style="width:150px;text-align: center">用户名</td>
<td style="width:200px;text-align: center">密码</td>
<td style="width:200px;text-align: center">邮箱号</td>
<td style="width:200px;text-align: center">手机号</td>
<td style="width:100px;text-align: center">操作</td>
</tr>
<c:forEach items="${list_usermessage}" var="user" varStatus="i">
<tr style="background:#7FFFD4">
<td style="width:50px;text-align: center">${i.count} </td>
<input type="hidden" name="c_id" id="c_id" value="${user.u_id} ">
<td style="width:100px;text-align: center">${user.u_name}</td>
<td style="text-align: center"><font style="font-size:12px;">${user.u_password}</font></td>
<td style="text-align: center">${user.u_email}</td>
<td style="width:100px;text-align: center">${user.u_phone}</td>
<td style="text-align: center"><input type="submit" value="删除"></td>
</tr>
</c:forEach>
</table>
方式二:超链接提交(传递参数要参数进行拼接)
<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序号</td>
<td style="width:150px;text-align: center">用户名</td>
<td style="width:200px;text-align: center">密码</td>
<td style="width:200px;text-align: center">邮箱号</td>
<td style="width:200px;text-align: center">手机号</td>
<td style="width:100px;text-align: center">操作</td>
</tr>
<c:forEach items="${list_usermessage}" var="user" varStatus="i">
<tr style="background:#7FFFD4">
<td style="width:50px;text-align: center">${i.count} </td>
<td style="width:100px;text-align: center">${user.u_name}</td>
<td style="text-align: center"><font style="font-size:12px;">${user.u_password}</font></td>
<td style="text-align: center">${user.u_email}</td>
<td style="width:100px;text-align: center">${user.u_phone}</td>
<td style="text-align: center"><a href="UserMesgServlet?method=deleteuser&u_id=${user.u_id}" target="middle">删除</a></td>
</tr>
</c:forEach>
</table>