You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookStore/src/com/yj/web/CartServlet.java

115 lines
5.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 导入所需的包和类
package com.yj.web;
import com.google.gson.Gson;
// 用于将Java对象转换为JSON字符串
import com.yj.bean.Book;
// 书籍实体类
import com.yj.bean.Cart;
// 购物车实体类
import com.yj.bean.CartItem;
// 购物车商品项实体类
import com.yj.service.BookService;
// 书籍服务接口
import com.yj.service.impl.BookServiceImpl;
// 书籍服务接口的实现类
import com.yj.utils.WebUtils;
// 工具类用于处理Web请求中的常见操作
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
// 定义一个Servlet类用于处理与购物车相关的请求
public class CartServlet extends BaseServlet {
// 创建一个书籍服务实例,用于访问书籍数据
private BookService bookService = new BookServiceImpl();
// 处理通过AJAX方式添加商品到购物车的请求
protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从请求中获取书籍ID并转换为整数类型默认为0
int id = WebUtils.parseInt(req.getParameter("id"), 0);
// 根据书籍ID查询书籍信息
Book book = bookService.queryBookById(id);
// 创建一个购物车商品项实例数量为1单价和总价均为书籍价格
CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
// 从会话中获取购物车对象,如果不存在则创建一个新的购物车对象
Cart cart = (Cart) req.getSession().getAttribute("cart");
if (cart == null) {
cart = new Cart();
req.getSession().setAttribute("cart", cart);
}
// 将商品项添加到购物车中
cart.addItem(cartItem);
// 在会话中保存最后添加的商品名称(可能用于显示或提示)
req.getSession().setAttribute("lastName", cartItem.getName());
// 准备返回给客户端的数据,包括购物车总数量和最后一个商品的名称
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("totalCount", cart.getTotalCount());
resultMap.put("lastName", cartItem.getName());
// 使用Gson将Map对象转换为JSON字符串
Gson gson = new Gson();
String resultMapJsonString = gson.toJson(resultMap);
// 将JSON字符串写入响应中
resp.getWriter().write(resultMapJsonString);
}
// 处理添加商品到购物车的请求非AJAX方式
protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 以下代码与ajaxAddItem方法中的代码大致相同只是最后返回的是页面重定向
int id = WebUtils.parseInt(req.getParameter("id"), 0);
Book book = bookService.queryBookById(id);
CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
Cart cart = (Cart) req.getSession().getAttribute("cart");
if (cart == null) {
cart = new Cart();
req.getSession().setAttribute("cart", cart);
}
cart.addItem(cartItem);
req.getSession().setAttribute("lastName", cartItem.getName());
// 重定向到请求来源页面通常是通过Referer头部获取的
resp.sendRedirect(req.getHeader("Referer"));
}
// 处理删除购物车中商品项的请求
protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从请求中获取要删除的商品项ID
int id = WebUtils.parseInt(req.getParameter("id"), 0);
// 从会话中获取购物车对象
Cart cart = (Cart) req.getSession().getAttribute("cart");
// 如果购物车对象存在则删除指定ID的商品项
if (cart != null) {
cart.deleteItem(id);
}
// 重定向到请求来源页面
resp.sendRedirect(req.getHeader("Referer"));
}
// 处理清空购物车中所有商品项的请求
protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从会话中移除购物车对象,从而清空购物车
req.getSession().removeAttribute("cart");
// 重定向到请求来源页面
resp.sendRedirect(req.getHeader("Referer"));
}
// 处理修改购物车中商品项数量的请求
protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从请求中获取要修改的商品项ID和新的数量
int id = WebUtils.parseInt(req.getParameter("id"), 0);
int count = WebUtils.parseInt(req.getParameter("count"), 1);
// 从会话中获取购物车对象
Cart cart = (Cart) req.getSession().getAttribute("cart");
// 如果购物车对象存在则修改指定ID的商品项数量
if (cart != null) {
cart.updateCount(id, count);
}
// 重定向到请求来源页面
resp.sendRedirect(req.getHeader("Referer"));
}
}