1、在JSP页面的<a>标签中将href指向对删除业务进行操作的doDeleteServlet,并传递需要删除的行的id
<a href="doDeleteServlet?id=当前行数据的id">删除</a>
<form>
<table width="95%" border="1" cellpadding="2" cellspacing="1" style="table-layout:fixed;">
<caption>用户信息</caption>
<tr>
<td width="80px" align="center" nowrap>用 户 id </td>
<td width="150px" nowrap>地 址</td>
<td width="180px" nowrap> email </td>
<td width="100px" nowrap>用 户 名</td>
<td width="100px" nowrap>用 户 密 码</td>
<td width="100px" nowrap>手 机 号 码</td>
<td width="100px" align="center" nowrap>用 户 类 型</td>
<td width="80px" align="center" nowrap>操 作</td>
</tr>
<c:forEach items="${userList}" var="users" step="1" varStatus="xh">
<tr>
<td >${users.userId}</td>
<td>${users.address}</td>
<td>${users.email}</td>
<td>${users.userName}</td>
<td>${users.passWd}</td>
<td>${users.phone}</td>
<td>${users.type}</td>
<td><a href="<%=request.getContextPath()%>/UserDeleteServlet?id=${users.userId}">删 除</a></td>
</tr>
</c:forEach>
</table>
</form>
2、在doDeleteServlet的doGet()方法中,调用dao中的删除方法
3、删除后,调用“取列表的”servlet,该servlet调用dao中的查询方法, 最终转到就最开始的jsp页面
@WebServlet("/UserDeleteServlet")
public class UserDeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
System.out.println(request.getParameter("id")+"-----------------");
int result = UsersDao.delete(request.getParameter("id"));
if(result>0){
System.out.println(result+"-----------操作成功");
}else{
System.out.println("操作失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page = null;
try {
request.setAttribute("userList", UsersDao.getAllUsers());
} catch (SQLException e) {
e.printStackTrace();
}
page = "/user/usersForeach.jsp";
getServletContext().getRequestDispatcher(page).forward(request,response);
}
}