利用session实现简单购物车功能

 更新时间:2022年02月09日 09:56:14   作者:一只小小的蚂蚁  
这篇文章主要为大家详细介绍了利用session实现简单购物车功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了利用session实现简单购物车功能的具体代码,供大家参考,具体内容如下

一、实现的功能

(1) 利用session实现购物车中的物品添加。
(2)使用servlet实现添加物品的功能(交互层)。
(3)一共有三个界面。第一个用来显示物品的列表的页面,第二个用来显示添物品的界面,第三个用来显示添加到购物车的信息页面。

二、代码实现

(1)物品列表页面:productlist.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R7'">联想拯救者R7</a>
    <br><br>
    <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R8'">联想拯救者R8</a>
    <br><br>
    <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R9'">联想拯救者R9</a>
    <br><br>
    <a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R10'">联想拯救者R10</a>
    <br><br>
</body>
</html>

(2)添加购物车页面:producttails.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
        <%
            String pname = (String)request.getAttribute("p");
            out.println(pname);
        %>
        <br><br>
        拿到其他的......该产品的详细参数
        
        <br><br>
        <a style="display: block;width: 100px ; height: 35px ;line-height :35px; text-decoration : none;background: red; color:#fff ;text-align: center;" href="<%=request.getContextPath() %>/addcart.pdo?pname=<%=pname %>" >加入购物车</a>
</body>
</html>

(3)显示添加成功后的信息页面:shoppingcart.jsp

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
        <%
            List<String> products = (List<String>)session.getAttribute("car");
            for(String s: products){
                out.print(s+"<br><br>");
            }
        %>
</body>
</html>

(4)交互层:ShopController.java

package com.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

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;

/**
 * Servlet implementation class ShopController
 */
@WebServlet(urlPatterns = {"*.pdo"})
public class ShopController extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShopController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("text/html; charset=utf-8");
        
        //在这个方法里边处理所有的增删改查的功能
        String mn = request.getServletPath();
        mn = mn.substring(1);
        mn = mn.substring(0 , mn.length()-4);
        
        //利用反射
        try {
            //获取方法名,并且根据具体的去调用下边的方法
            Method method = this.getClass().getDeclaredMethod(mn,HttpServletRequest.class , HttpServletResponse.class);
            method.invoke(this,request,response);
            
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
    
    private void shopping (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("text/html; charset=utf-8");
        String pname = request.getParameter("pname");
        request.setAttribute("p", pname);
        request.getRequestDispatcher("/producttails.jsp").forward(request, response);;
    }
    
    private void addcart (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("text/html; charset=utf-8");
        
        String pname = request.getParameter("pname");
        //添加购物车
        HttpSession session = request.getSession(true);
        
        List<String> products = (List<String>)session.getAttribute("car");
        if(products == null) {
            products = new ArrayList<>();
        }
        //把添加的物品放入List集合
        products.add(pname);
        //放入session中表示添加成功
        session.setAttribute("car", products);
        
        //response.getWriter().print("添加成功!");
        response.sendRedirect(request.getContextPath() + "/shoppingcart.jsp");
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis利用association或collection传递多参数子查询

    mybatis利用association或collection传递多参数子查询

    今天小编就为大家分享一篇关于mybatis利用association或collection传递多参数子查询,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Java数据库连接池的几种配置方法(以MySQL数据库为例)

    Java数据库连接池的几种配置方法(以MySQL数据库为例)

    这篇文章主要介绍了Java数据库连接池的几种配置方法(以MySQL数据库为例) 的相关资料,需要的朋友可以参考下
    2016-07-07
  • Java中的信号量Semaphore详细解读

    Java中的信号量Semaphore详细解读

    这篇文章主要介绍了Java中的信号量Semaphore详细解读,Java信号量机制可以用来保证线程互斥,创建Semaphore对象传入一个整形参数,类似于公共资源,需要的朋友可以参考下
    2023-11-11
  • java中使用@Transactional会有哪些坑

    java中使用@Transactional会有哪些坑

    在Java中,@Transactional是一个常用的注解,用于声明方法应该在一个事务的上下文中执行,本文主要介绍了java中使用@Transactional会有哪些坑,感兴趣的可以了解一下
    2024-04-04
  • JavaWeb学习笔记分享(必看篇)

    JavaWeb学习笔记分享(必看篇)

    下面小编就为大家带来一篇JavaWeb学习笔记分享(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • SpringMVC异常处理的三种方式小结

    SpringMVC异常处理的三种方式小结

    本文主要介绍了SpringMVC异常处理的三种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-09-09
  • eclipse老是自动跳到console解决办法

    eclipse老是自动跳到console解决办法

    eclipse启动服务后,想看一些properties信息或者别的,但老是自动跳转到console页面,本文给大家介绍了解决办法,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-03-03
  • Java中ReentrantLock4种常见的坑

    Java中ReentrantLock4种常见的坑

    本文主要介绍了Java中ReentrantLock 4种常见的坑,ReentrantLock默认情况下为非公平锁,下文关于其更多详情需要的小伙伴可以参考一下
    2022-05-05
  • Java不定参数使用及一些注意情况

    Java不定参数使用及一些注意情况

    不定参数是一种特殊的参数类型,它允许方法接受可变数量的参数,本文主要介绍了Java不定参数使用及一些注意情况,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • mybatis接收以逗号分隔的字符串批量查询方式

    mybatis接收以逗号分隔的字符串批量查询方式

    这篇文章主要介绍了mybatis接收以逗号分隔的字符串批量查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论