关于对session的理解通俗语句结合实例

本文通过通俗语句和实例解析session的工作原理,将session比喻为Map集合,强调了sessionID在用户请求中的作用。在编程中,session.getAttribute('cart')的'cart'是自定义名称,用于存储和检索数据。
摘要由CSDN通过智能技术生成

关于对session的理解通俗语句结合实例

这个其实是 因为在session会创建一个唯一的id号给服务器,当用户请求时 会从服务器中获取id号
        注:1.我们可以把session想成map集合
           2.有些人对 Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");
           中的getAttribute("cart")里面的cart 不是很理解 先解释如下 其实就是 cart是自己创建的 随意起的名字与任何页面都无关联关系
            原因就是刚开始访问网站时会自动生成一个session,这个session为空,所以通过if(cart==null)把我们的sessin里面的东西补充起来
            
 

package com.zhiliaotang.servlet;

import com.zhiliaotang.model.CartItem;
import com.zhiliaotang.model.Product;
import com.zhiliaotang.service.ProductService;
import com.zhiliaotang.service.impl.ProductServiceImpl;

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.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/product.do")
public class ProductServlet extends HttpServlet {
    //调用业务层
    private ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置请求参数编码
        req.setCharacterEncoding("UTF-8");
        //获取请求参数
        String method = req.getParameter("method");

        if(null==method){
            //查询所有商品
            list(req,resp);
        }else if("view".equals(method)){
            //查看商品详情
            view(req,resp);
        }else if("cart".equals(method)){
            //加入购物车
            addCart(req,resp);
        }
    }

    private void addCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取编号
        String id = req.getParameter("id");
        //根据编号获取商品对象
        Product product = productService.findProductById(Integer.parseInt(id));
        //获取会话
        HttpSession session = req.getSession();
        //从会话中获取购物车对象
        Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");
       /*这个其实是 因为在session会创建一个唯一的id号给服务器,当用户请求时 会从服务器中获取id号
        注:1.我们可以把session想成map集合
           2.有些人对 Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");
           中的getAttribute("cart")里面的cart 不是很理解 先解释如下 其实就是 cart是自己创建的 随意起的名字与任何页面都无关联关系
            原因就是刚开始访问网站时会自动生成一个session,这个session为空,所以通过if(cart==null)把我们的sessin里面的东西补充起来
            
        * */
        if(cart==null){
            cart = new HashMap<Integer, CartItem>();
            //将购物车放入会话中
            session.setAttribute("cart",cart);
        }
        CartItem cartItem = null;
        //判断购物中是否存在该商品
        if(cart.containsKey(product.getId())){
            //存在此商品
            cartItem = cart.get(product.getId());
            //累加数据
            cartItem.setCount(cartItem.getCount()+1);
        }else{
            //购物车没有此商品
            cartItem =  new CartItem(product,1);
        }
        //将购物车项添加到购物车中
        cart.put(product.getId(),cartItem);
        //重定向到购物车页面
        resp.sendRedirect("product_cart.jsp");
    }

    private void view(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        //获取编号
        String id = req.getParameter("id");
        //根据编号获取商品对象
        Product product = productService.findProductById(Integer.parseInt(id));
        //将商品入在请求属性中
        req.setAttribute("product",product);
        //页面转发
        req.getRequestDispatcher("product_view.jsp").forward(req,resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        //获取会话
        HttpSession session = req.getSession();

        List<Product> products = productService.findAllProducts();
        //将产品集合放入会话中
        session.setAttribute("products",products);
        //页面重定向
        resp.sendRedirect("product_list.jsp");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值