博客
关于我
Session_细节_hehe.employment.over.16.6
阅读量:386 次
发布时间:2019-03-05

本文共 1570 字,大约阅读时间需要 5 分钟。

Session技术细节说明

16.13.1 当客户端关闭后,服务器不关闭,两次获取session是否为同一个?

默认情况下,获取的不是同一个Session实例。

如果需要确保获取的Session实例相同,可以通过创建带有JSESSIONID值的Cookie并设置其最大有效期来实现持久化。

示例代码:

package com.xww.session;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpSession;import java.io.IOException;@WebServlet("/sessionDemo3")public class SessionDemo3 extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // 获取Session        HttpSession session = request.getSession();        System.out.println(session);        // org.apache.catalina.session.StandardSessionFacade@63bfc40                // 创建持久化Cookie        Cookie c = new Cookie("JSESSIONID", session.getId());        c.setMaxAge(60 * 60);        response.addCookie(c);    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        this.doPost(request, response);    }}

16.13.2 客户端不关闭,服务器关闭后,两次获取的Session是否为同一个?

服务器关闭后,两次获取的Session不是同一个,但需要注意以下事项以确保数据安全:

  • Session钝化:在服务器正常关闭前,将Session对象系列化到硬盘上。
  • Session活化:服务器重新启动时,将Session文件转化为内存中的Session对象。

通过上述方法,可以确保Session数据在服务器关闭后仍能被正确恢复。

16.13.3 Session何时被销毁?

Session会在以下情况下被销毁:

  • 服务器关闭;
  • Session调用invalidate()方法;
  • Session达到默认失效时间(30分钟)。
  • 需要注意的是,默认失效时间可以通过修改web.xml配置文件来更改。

    示例配置:

    30

    16.13.4 Session的特点

  • Session用于存储一次会话中多次请求的数据,存在于服务器端;
  • Session可以存储任意类型、任意大小的数据;
  • Session与Cookie的主要区别包括:
    • Session存储数据在服务器端,Cookie存储在客户端;
    • Session无数据大小限制,Cookie有数据大小限制;
    • Session数据更安全,Cookie较不安全。
  • 转载地址:http://uugwz.baihongyu.com/

    你可能感兴趣的文章
    Node JS: < 一> 初识Node JS
    查看>>
    Node JS: < 二> Node JS例子解析
    查看>>
    Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node 裁切图片的方法
    查看>>
    node+express+mysql 实现登陆注册
    查看>>
    Node+Express连接mysql实现增删改查
    查看>>
    node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
    查看>>
    Node-RED中Button按钮组件和TextInput文字输入组件的使用
    查看>>
    vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
    查看>>
    Node-RED中Switch开关和Dropdown选择组件的使用
    查看>>
    Node-RED中使用exec节点实现调用外部exe程序
    查看>>
    Node-RED中使用function函式节点实现数值计算(相加计算)
    查看>>
    Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用node-red-contrib-image-output节点实现图片预览
    查看>>
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>