简单实现将商品加入购物车(初级)

目录

以四大名著为列,简单实现将书名加入到购物车中,过程中每加入一次会提示加入购物车成功

pojo类(Book)

 product.jsp(产品展示页面)

success.jsp(加入购物车提示页面)

shoppingcar.jsp(查看购物车)

 web.xml(服务器启动后跳转的第一页面)


 

以四大名著为列,简单实现将书名加入到购物车中,过程中每加入一次会提示加入购物车成功

package com.cdy.session;

import com.cdy.pojo.Book;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/*
 * @Project:session_demo
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/2/7 11:08
 * */
@WebServlet("/")
public class ShoppingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("shoppingServlet");

        //获取bookname
        String bookname = request.getParameter("bookname");

        //把获取到的bookname封装成Book对象
        Book book = new Book();
        book.setBookname(bookname);

        //请求session   获取session
        HttpSession session = request.getSession();

        //创建集合list,存储封装完成的对象信息,从session中获取信息
        ArrayList<Book> list1 = (ArrayList) session.getAttribute("list");

        //判断list集合中是否为空,则第一次创建list集合
        if (list1==null){
            ArrayList<Book> list = new ArrayList<>();
            list.add(book);
            //添加到session中
            session.setAttribute("list",list);
        }else{
            list1.add(book);
            //添加到session中
            session.setAttribute("list",list1);

        }
        request.getRequestDispatcher("success.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

pojo类(Book)

package com.cdy.pojo;

import lombok.Data;

/*
 * @Project:session_demo
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/2/7 11:20
 * */

public class Book {
    private String bookname;

    public Book() {
    }

    public Book(String bookname) {
        this.bookname = bookname;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookname='" + bookname + '\'' +
                '}';
    }
}

 

 product.jsp(产品展示页面)

<%--
  User: cdy
  Date: 2023/2/7 10:55
  @Version 1.0
  @Motto:放下杂念,只为迎接明天更好的自己
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书店</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/shoppingServlet?bookname=《三国演义》">《三国演义》</a>
<a href="${pageContext.request.contextPath}/shoppingServlet?bookname=《水浒传》">《水浒传》</a>
<a href="${pageContext.request.contextPath}/shoppingServlet?bookname=《西游记》">《西游记》</a>
<a href="${pageContext.request.contextPath}/shoppingServlet?bookname=《红楼梦》">《红楼梦》</a><br>
<a href="${pageContext.request.contextPath}/shoppingcar.jsp">查看购物车
</a>
</body>
</html>

success.jsp(加入购物车提示页面)

<%--
  User: cdy
  Date: 2023/2/7 11:06
  @Version 1.0
  @Motto:放下杂念,只为迎接明天更好的自己
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
<h1>加入购物车成功</h1>

</body>
</html>

shoppingcar.jsp(查看购物车)

<%--
  User: cdy
  Date: 2023/2/7 15:56
  @Version 1.0
  @Motto:放下杂念,只为迎接明天更好的自己
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
</head>
<body>
${list}
</body>
</html>

 web.xml(服务器启动后跳转的第一页面)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
<!--服务器启动后跳转的第一页面-->
    <welcome-file-list>
        <welcome-file>product.jsp</welcome-file>
    </welcome-file-list>
</web-app>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值