删除购物项
Listcart.jsp
新建DemeteByIdServlet.java
package www.hbsi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import www.hbsi.domain.Cart;
import www.hbsi.service.BookService;
import www.hbsi.service.BookServiceImpl;
public class DeleteByIdServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ID
String id = request.getParameter("id");
//删除购物车里的购物项,获取车
Cart cart = (Cart) request.getSession().getAttribute("cart");
//调用方法,从购物项里删除书
BookService service = new BookServiceImpl();
service.deleteById(id,cart);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
1. 创建删除服务项BookService:
package www.hbsi.service;
import java.util.List;
import www.hbsi.domain.Book;
import www.hbsi.domain.Cart;
public interface BookService {
//查询所有书籍
public List<Book> getAll();
//根据ID查找书籍
public Book findById(String id);
//删除购物项
public void deleteById(String id, Cart cart);
}
2. 实现删除服务项BookServiceImpl:
package www.hbsi.service;
import java.util.List;
import www.hbsi.dao.BookDao;
import www.hbsi.dao.BookDaoImpl;
import www.hbsi.domain.Book;
import www.hbsi.domain.Cart;
public class BookServiceImpl implements BookService {
BookDao bd = new BookDaoImpl();
public Book findById(String id) {
// TODO Auto-generated method stub
return bd.findById(id);
}
public List<Book> getAll() {
// TODO Auto-generated method stub
return bd.getAll();
}
public void deleteById(String id, Cart cart) {
// TODO Auto-generated method stub
cart.getMap().remove(id);
}
}
DelectByIdServlet.java
package www.hbsi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import www.hbsi.domain.Cart;
import www.hbsi.service.BookService;
import www.hbsi.service.BookServiceImpl;
public class DeleteByIdServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ID
String id = request.getParameter("id");
//删除购物车里的购物项,获取车
Cart cart = (Cart) request.getSession().getAttribute("cart");
//调用方法,从购物项里删除书
BookService service = new BookServiceImpl();
service.deleteById(id,cart);
//转回购买界面
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
![](https://img-my.csdn.net/uploads/201211/21/1353502537_4652.png)
![](https://img-my.csdn.net/uploads/201211/21/1353502544_9497.png)
![](https://img-my.csdn.net/uploads/201211/21/1353502552_5726.png)
删除的时候加一个提示
listcard.Jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>购物车显示页面</title>
<script type="text/javascript">
function deletitem(id){
var b = window.confirm("确定要删除吗?");
if(b){
window.location.href="${pageContext.request.contextPath}/deletedById.do?id="+id;
}
}
</script>
</head>
<body style="text-align:center">
<h2>您购买了如下商品</h2>
<table border="1" width="80%">
<tr>
<td>编号</td>
<td>书名</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
<td>操作</td>
</tr>
<c:forEach var="me" items="${cart.map}">
<tr>
<td>${me.key}</td>
<td>${me.value.book.name}</td>
<td>${me.value.book.price}</td>
<td>${me.value.quantity}</td>
<td>${me.value.price}</td>
<td>
<!-- 删一个购物项不是购物车 -->
<a href="javascript:deletitem(${me.key})">删除</a>
<!-- <a href="${pageContext.request.contextPath}/deletedById.do?id=${me.key}">删除</a>-->
</td>
</tr>
</c:forEach>
<tr>
<td colspan="3">总价</td>
<td colspan="2">${cart.price}</td>
</tr>
</table>
</body>
</html>
![](https://img-my.csdn.net/uploads/201211/21/1353502618_5986.png)
![](https://img-my.csdn.net/uploads/201211/21/1353502630_3654.png)
![](https://img-my.csdn.net/uploads/201211/21/1353502640_6168.png)