实验六 实现购物车
一、实验目的
1. 理解Session的作用;
将Session应用到具体问题中。二、实验内容
当用户使用浏览器访问网站的蛋糕列表页面,如果想购买某个蛋糕,首先会判断蛋糕是否存在,如果存在就加入购物车,跳转到购物车中所购买蛋糕的离了表页面,否则,返回蛋糕列表页面,流程如下:
创建蛋糕类,封装蛋糕信息;创建CakeDB类,模拟保存所有蛋糕的数据库;创建ListCakeServlet的Servlet类,该Servlet用于显示所有可购买的蛋糕的列表,单击“点击购买”链接,便可以将指定的蛋糕添添加到购物车中;创建一个名称为PurchaseServlet的Servlet类,该类实现了两个功能,一个是将用户购买的蛋糕信息保存到Session对象中,一个是在用户购买蛋糕结束后,将页面重定向到用户已经购买的蛋糕列表;创建一个名称为CartServlet的Servlet类,该类主要用于展示用户已经购买的蛋糕列表。运行结果示例如图:
三、实验结果(源代码、运行截图)
Cake.java
package xxx;public class Cake { private int id; private String name; public Cake(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; }
CakeDB.java
package xxx;import java.util.ArrayList;import java.util.List;public class CakeDB { private static List<Cake> cakes = new ArrayList<>(); static { // 初始化蛋糕列表,这里仅为示例,应从实际数据库加载 cakes.add(new Cake(1, "A类蛋糕")); cakes.add(new Cake(2, "B类蛋糕")); cakes.add(new Cake(3, "C类蛋糕")); cakes.add(new Cake(4, "D类蛋糕")); cakes.add(new Cake(5, "E类蛋糕")); } public static List<Cake> getAllCakes() { return cakes; }}
CartServlet.java
package xxx;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class CartServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Cake> cart = (List<Cake>) req.getSession().getAttribute("CartServlet"); if (cart == null) { cart = new ArrayList<>(); } req.setAttribute("CartServlet", cart); RequestDispatcher dispatcher = req.getRequestDispatcher("cart.jsp"); dispatcher.forward(req, resp); }}
ListCakeServlet.java
package xxx;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;import java.util.List;public class ListCakeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Cake> cakes = CakeDB.getAllCakes(); req.setAttribute("cakes", cakes); RequestDispatcher dispatcher = req.getRequestDispatcher("cake_list.jsp"); dispatcher.forward(req, resp); }}
PurchaseServlet.java
package xxx;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class PurchaseServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int cakeId = Integer.parseInt(req.getParameter("id")); int flag=0; System.out.println(cakeId); List<Cake> cart = (List<Cake>) req.getSession().getAttribute("CartServlet"); if (cart == null) { cart = new ArrayList<>(); req.getSession().setAttribute("CartServlet", cart); } Cake cake = CakeDB.getAllCakes().stream() .filter(c -> c.getId() == cakeId) .findFirst() .orElse(null); if (cake != null) { cart.add(cake); } resp.sendRedirect("CartServlet"); }}
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>ListCakeServlet</servlet-name> <servlet-class>xxx.ListCakeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListCakeServlet</servlet-name> <url-pattern>/ListCakeServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>PurchaseServlet</servlet-name> <servlet-class>xxx.PurchaseServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PurchaseServlet</servlet-name> <url-pattern>/purchase</url-pattern> </servlet-mapping> <servlet> <servlet-name>CartServlet</servlet-name> <servlet-class>xxx.CartServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CartServlet</servlet-name> <url-pattern>/CartServlet</url-pattern> </servlet-mapping></web-app>
cake_list.jsp
<%@ page import="java.util.List" %><%@ page import="xxx.Cake" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>蛋糕列表</title></head><body>本站提供的蛋糕有<br> <% for (Cake cake : (List<Cake>)request.getAttribute("cakes")) { %> <%=cake.getName()%> <a href="purchase?id=<%=cake.getId()%>">点击购买</a><br> <% } %> <a href="CartServlet">查看购物车</a></body></html>
cart.jsp
<%@ page import="java.util.List" %><%@ page import="xxx.Cake" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>购物车</title></head><body>您购买的蛋糕有<br> <% List<Cake> cart = (List<Cake>)request.getAttribute("CartServlet"); if (cart != null && !cart.isEmpty()) { for (Cake cake : cart) { %> <%= cake.getName() %><br> <% } } else { %> 购物车为空 <% } %><a href="ListCakeServlet">返回蛋糕列表</a></body></html>