欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用session实现简单的购物车

程序员文章站 2024-03-20 13:29:58
...

列出所有书籍,用一个类来模拟数据库

//代表网站首页,列出所有书
@WebServlet(name = "ListBookServlet",urlPatterns = "/ListBookServlet")
public class ListBookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        out.print("本网站有如下商品:<br/>");
        Map<String , book> map = Db.getAll();

        for(Map.Entry<String , book> entry : map.entrySet()){
            book book = entry.getValue();
            out.print(book.getName() + "<a href = '/BuyServlet?id="+book.getId()+ "'_target = _blank'>购买</a>");
        }
    }
}
    class Db{
        private static Map<String , book> map = new LinkedHashMap();
        static{
            map.put("1",new book("1","javaweb","aa","good"));
            map.put("2",new book("2","jdbc","bb","good"));
            map.put("3",new book("3","jvm","cc","good"));
            map.put("4",new book("4","c#","dd","good"));
            map.put("5",new book("5","spring","ee","good"));
        }
        public static Map getAll(){

            return map;
        }

    }
    class book{
        private String id;
        private String name;
        private String author;
        private String description;

        public book(String id, String name, String author, String description) {
            this.id = id;
            this.name = name;
            this.author = author;
            this.description = description;
        }

        public book() {
        }

        public String getName() {

            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getId() {

            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

}

购买

//购买
@WebServlet(name = "BuyServlet",urlPatterns = "/BuyServlet")
public class BuyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

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

        String id = request.getParameter("id");
        book book = (book) Db.getAll().get(id);

        HttpSession session = request.getSession();

        //从session得到用户用于保存所有书的集合(购物车)
        List list = (List) session.getAttribute("list");
        if(list == null){
            list = new ArrayList();
            session.setAttribute("list" , list);
        }
        list.add(book);

        //request.getRequestDispatcher("/ListCarServlet").forward(request,response); 转发,刷新后会重新执行当前操作,不可用
        response.sendRedirect("ListCarServlet");


    }
}

显示用户购买的商品

//显示用户购买的商品
@WebServlet(name = "ListCarServlet",urlPatterns = "/ListCarServlet")
public class ListCarServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

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

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        if(session == null){
            out.print("您没有购买任何商品");
            return;
        }
        out.write("您购买了如下商品 <br/>");
        List<book> list = (List<book>) session.getAttribute("list");
        for(book book : list){
            out.write(book.getName());
        }

    }
}