package cn.itcast.estore.web.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.estore.domain.Product;
public class AddCountInCartServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取参数id,商品数量count
String id = request.getParameter("id");
String count = request.getParameter("count");
int countInt = Integer.parseInt(count);
// 2.获取购物车
Map<Product, Integer> cart = (Map<Product, Integer>) request
.getSession().getAttribute("cart");
// 3.遍历购物车cart,判断购物车中是否有该商品
if (cart != null) {
for (Product p : cart.keySet()) {
if (p.getId().equals(id)) {
// 4.count++ 判断是否超出库存
if (countInt >= p.getNum()) {
countInt = p.getNum();
request.setAttribute("add.message", "已达到库存最大值!");
} else {
countInt++;
}
// 5.将商品添加到购物车,添加到session
cart.put(p, countInt);
}
}
// 6.将cart添加到session
request.getSession().setAttribute("cart", cart);
request.getRequestDispatcher("/showCart.jsp").forward(request,
response);
return;
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
showCart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>购物车信息</title>
<script type="text/javascript">
//商品id,数量,库存
function changeCount(id, count, num) {
//将count转换为数值型,num本身就是数字
count = parseInt(count);
//边界控制
if (count > num) {
count = num;
alert("最大购物数量:" + num);
} else if (count == 0) {
count = 0
var flag = window.confirm("确定要删除该商品吗?");
if (flag) {
count = 0;
} else {
count = 1;
}
}
location.href = "${pageContext.request.contextPath}/updateCountInCart?id="
+ id + "&count=" + count;
};
function numberText(e) {
var keyCode;
if (e && e.preventDefault) {
//判断是firefox浏览器
keyCode = e.which;
} else {
//ie浏览器
keyCode = window.event.keyCode;
}
//alert(keyCode);
//0-9之间的键码值是48-57
if (!(keyCode >= 48 && keyCode <= 57 || keyCode == 8)) {
//阻止事件的默认行为
if (e && e.preventDefault) {
// e对象存在,preventDefault方法存在 ---- 火狐浏览器
e.preventDefault();
} else {
// 不支持e对象,或者没有preventDefault方法 ---- IE
window.event.returnValue = false;
}
}
};
//通过阻止事件的默认行为来实现控制删除
function preventDel(e) {
var flag = window.confirm("确定要删除商品吗"); //确定是返回ture,否返回false;
alert(flag);
if (!flag) {
//不删除,阻止链接的默认行为 执行。
//阻止事件的默认行为
//该方法是阻止事件的默认行为
if (e && e.preventDefault) {
//e对象存在 e.preventDefault方法存在---firfox浏览器
e.preventDefault();
} else {
//不支持e对象 或者preventDefault方法不存在----IE浏览器
window.event.returnValue = false;
}
}
}
function preventRemove(id) {
var flag = window.confirm("是否要删除商品");
if (flag) {
//确定返回ture 删除商品
location.href = "${pageContext.request.contextPath }/removeProductFromCrat?id="
+ id;
}
};
</script>
</head>
<body>
<c:if test="${empty cart }">
购物车里没有商品
</c:if>
<c:if test="${not empty cart }">
<br>
<br>
<br>
<table border='1' align='center' width="65%">
<caption>购物车</caption>
<tr>
<td>图片</td>
<td>商品名称</td>
<td>商品数量</td>
<td>操作</td>
</tr>
<c:set var="totalMoney" value="0" />
<c:forEach items="${cart }" var="mp">
<tr>
<td><img
src="${pageContext.request.contextPath }${mp.key.imgurl}" alt="图片"
align="left" height="50" width="50" /></td>
<td>${mp.key.pname }</td>
<td>${mp.value }<span>${requestScope["add.message"]}</span>
</td>
<td><input type="button" value="-"
οnclick="changeCount('${mp.key.id}','${mp.value-1}')"> <input
type="button" value="+"
οnclick="changeCount('${mp.key.id}','${mp.value+1}','${mp.key.num}')">
可购买数量:${mp.key.num} <input type="text"
value="${mp.value}" size="7" style="text-align:center"
οnblur="changeCount('${mp.key.id}',this.value,'${mp.key.num}')"
οnkeydοwn="numberText(event)">
</td>
<td><a
href="${pageContext.request.contextPath }/removeProductFromCrat?id=${mp.key.id }"
οnclick="preventDel(event);">删除</a> <a href="#"
οnclick="preventRemove('${mp.key.id }');">删除</a> <a href="#"
οnclick="preventRemove('${mp.key.id }');return false;">删除</a> <a
href="" οnclick="preventRemove('${mp.key.id }');return false;">删除</a>
<a href="javascript:;" οnclick="preventRemove('${mp.key.id }');">删除</a>
<a href="javascript:void(0);"
οnclick="preventRemove('${mp.key.id }');">删除</a>
</td>
</tr>
<c:set var="totalMoney"
value=" ${totalMoney + mp.key.price * mp.value}" />
</c:forEach>
<tr>
<td colspan="2" align="center">总价:</td>
<td>${totalMoney }</td>
</tr>
<tr>
<td><a href="${pageContext.request.contextPath }/order.jsp">结算</a>
</td>
</tr>
</table>
</c:if>
</body>
</html>
来源:CSDN
作者:live801
链接:https://blog.csdn.net/live801/article/details/103951415