Session

本文详细介绍了Servlet中的Session概念,包括Session的创建、获取、设置值、设置超时时间以及在购物车场景中的应用。Session用于维持客户端与服务器之间的会话状态,通常用于用户登录信息的持久化。通过示例代码展示了如何操作Session,并讨论了Session的生命周期和管理策略。
摘要由CSDN通过智能技术生成

Session概念

  1. Session是个接口
  2. Session意思是一种技术,用来维护客户端和服务端的关联
  3. 每个客户端都有自己的一个Session会话
  4. 经常用来保存用户登录信息
  5. 存在服务器内存中
  6. 我的理解:其实浏览器再第一次访问服务器(即不带任何Cookie)的时候,服务器就会通过Request.getSession()创建一个Session,同时创建一个Cookie,Cookie的key是 JSESSIONID,value是刚创建出来的SessionId,然后添加到头部返回给客户端,客户端接收到后如果本地没有该key的Cookie,则创建该Cookie并保存在本地,然后以后 的每次请求都会带这个Cookie,服务器取到这个Cookie的值(SessionId),把这个Session与服务器内存中存的SessionId比较,如果有则表明用户已经登录。 如果客户端关闭了,再次打开,请求服务器,此时又会创建一个新的Session给客户端,但是原来上次的Session没有超过过期时间,所以还在内存中,只有到过期时间,才 会销毁 

 

 Session的创建、获取(id,是否是新的)

/**
 * 1.request.getSession()
 *      第一次调用是:创建Session会话
 *      之后调用都是:获取前面创建好的Session会话对象
 * 2.isNew():判断是否是第一次创建的
 *      true 第一次创建的
 *      false 获取之前创建的
 * 3.getId():每个Session都有一个SessionId,是唯一的
 */
@GetMapping("/getSession")
public void getSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession();
    boolean isNew = session.isNew();
    String id = session.getId();
    
    response.setContentType("text/html;charset = UTF-8");
    response.getWriter().write("sessionId:" + id + "是否是新创建的:" + isNew);
}

获取、设置Session值 

/**
 * 设置Session的值
 */
@GetMapping("/setAttrbute")
public void setAttrbute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    request.getSession().setAttribute("key1","value1");
    response.setContentType("text/html;charset = UTF-8");
    response.getWriter().write("已存");
}

/**
 * 获取Session值
 */
@GetMapping("/getAttrbute")
public void getAttrbute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Object attrbute = request.getSession().getAttribute("key1");
    response.getWriter().write(String.valueOf(attrbute));
}

 获取、设置Session时长

  1. Session时长是指:两次客户端最小请求间隔时间,即超时时间内,没有客户端请求,Session才会超时
  2. 默认超时时长为30分钟,在Tomcat的web.xml配置里配置了
/**
 * 1.request.getSession().getMaxInactiveInterval():获取Session超时时长
 * 2.request.getSession().setMaxInactiveInterval(3):设置Session超时时间,单位是秒,填负数表示永不超时
 * 3.request.getSession().invalidate():使Session马上失效
 */
@GetMapping("/setLife")
public void setLife(HttpServletRequest request, HttpServletResponse response) throws IOException {
    int maxInactiveInterval = request.getSession().getMaxInactiveInterval();
    System.out.println("Session超时时长" + maxInactiveInterval);
    request.getSession().setMaxInactiveInterval(3);
    request.getSession().invalidate();
}

Session实现购物车

@GetMapping("/setShopCar")
public Map<String, Integer> getSessionId(HttpServletRequest request, @RequestParam int id){
    HttpSession session = request.getSession();
    String[] strings = new String[]{"电视","冰箱","洗衣机","电风扇","空调"};
    String shop = strings[id - 1];
    Map<String, Integer> shopCar = (Map<String, Integer>) session.getAttribute("shopCar");
    if (Objects.nonNull(shopCar)) {
        shopCar.computeIfAbsent(shop, value -> 0);
        shopCar.put(shop, shopCar.get(shop) + 1);
    }else {
        shopCar = new HashMap<>();
        shopCar.put(shop, 1);
        session.setAttribute("shopCar", shopCar);
    }
    return shopCar;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月下未来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值