目录
3.创建一个servlet,用来实现购物车的添加和删除功能。
1.创建一个Auction类,用来描述商品信息。
public class Auction {
private String id;
private String title;
private String description;
private float price;
public AuctionDO(String title, String disc, float price) {
this.title = title;
this.description = disc;
this.price = price;
this.id = UUID.randomUUID().toString();
}
public AuctionDO() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String toString() {
return "AuctionDO[id=" + id + ",title=" + title + ",description=" +
description + ", price=" + price + "]";
}
}
2.创建一个jsp页面,用来显示商品信息。
<%@ page import="cn.edu.zzti.entity.CartDO" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>我的购物车</title>
</head>
<body>
<table>
<tr>
<td colspan="" align="right">
<jsp:include page="../common/web/top.jsp"/>
</td>
</tr>
<tr valign="top">
<td width="20%">
<%@include file="../common/web/left.jspf" %>
</td>
<td align="center">
<form action="" method="post">
<table border="">
<tr>
<td colspan="" align="center">我的购物车</td>
</tr>
<tr>
<td></td>
<td>商品编号</td>
<td>商品名称</td>
<td>商品数量</td>
<td>商品单价</td>
<td>商品总价</td>
<td>操作列表</td>
</tr>
<%
Object o = request.getAttribute("cartList");
if (o != null) {
List<CartDO> list = (List<CartDO>) o;
for (int i = 0; i < list.size(); i++) {
%>
<tr>
<td>
<input type="checkbox" name="slctedOrder"
value="<%=list.get(i).getAuctionDO().getId()%>">
</td>
<td><%=list.get(i).getAuctionDO().getId()%>
</td>
<td><%=list.get(i).getAuctionDO().getTitle()%>
</td>
<td><%=list.get(i).getNumber()%>
</td>
<td><%=list.get(i).getAuctionDO().getPrice()%>
</td>
<td><%=list.get(i).getTotlePrice()%>
</td>
<td><a href="<%=pageContext.getServletContext().getContextPath()%>/web/RemoveCartServlet?id=<%=list.get(i).
getAuctionDO().getId()%>">删除此商品</a></td>
</tr>
<%
}
}
%>
<tr>
<td colspan="" align="right">
<a href="<%=pageContext.getServletContext().
getContextPath()%>/web/getAllAuction">
继续购物</a></td>
</tr>
</table>
</form>
</td>
<tr>
</table>
</html>
3.创建一个servlet,用来实现购物车的添加和删除功能。
3.1添加购物车
@WebServlet(name = "AddCartServlet",
urlPatterns = {PathConstence.W_SERVLET_BASE + "/AddCartServlet"})
public class AddCartServlet extends HttpServlet {
private TemporaryCartDAO temporaryCartDAO = (TemporaryCartDAO)
DAOFactory.getDAO(DAOFactory.TEMPORARY_CART_DAO_CLASS_NAME);
private AuctionDAO auctionDAO = (AuctionDAO)
DAOFactory.getDAO(DAOFactory.AUCTION_DAO_CLASS_NAME);
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String basePath =
request.getContextPath() + PathConstence.W_SERVLET_BASE;
String aucId = request.getParameter("id");
if (aucId == null || "".equals(aucId)) {
response.sendRedirect(basePath + "/getAllAuctionServlet");
}
try {
AuctionDO auctionDO = auctionDAO.getAuction(aucId);
this.temporaryCartDAO.addToCart(((UserDO) request.
getSession().getAttribute("user")).getUsername(), auctionDO);
response.sendRedirect(basePath + "/CartListServlet");
} catch (SQLException e) {
e.printStackTrace();
response.sendRedirect(basePath + "/getAllAuctionServlet");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
3.2删除购物车
package cn.edu.zzti.servlet;
import cn.edu.zzti.dao.DAOFactory;
import cn.edu.zzti.dao.TemporaryCartDAO;
import cn.edu.zzti.entity.UserDO;
import cn.edu.zzti.util.PathConstence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "RemoveCartServlet",
urlPatterns = {PathConstence.W_SERVLET_BASE + "/RemoveCartServlet"})
public class RemoveCartServlet extends HttpServlet {
private TemporaryCartDAO temporaryCartDAO = (TemporaryCartDAO)
DAOFactory.getDAO(DAOFactory.TEMPORARY_CART_DAO_CLASS_NAME);
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String aucId = request.getParameter("id");
temporaryCartDAO.removeFromCart(
((UserDO) request.getSession().getAttribute("user"))
.getUsername(), aucId);
response.sendRedirect(request.getContextPath() + PathConstence.W_SERVLET_BASE + "/CartListServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}