当前位置:网站首页>19. Server-side session technology Session
19. Server-side session technology Session
2022-08-05 09:32:00 【Zhang San who is learning java】
目录
3.1客户端关闭后,服务器不关闭,两次获取到的session不是同一个
3.2客户端不关闭,服务器关闭后,两次获取到的session不是同一个
一、服务器端会话技术Session
服务器端会话技术,在一次会话的多次请求之间共享数据,将数据保存在服务器端的对象中,HttpSession.
1.Session的实现原理
Session的实现是依赖于Cookie的

2.Session使用步骤
先获取HttpSession对象:
HttPSession session =request.getSession();
使用HttpSession对象
Object getAttribute(String name)
void setAttribute(String name,Object value)
void removeAttribute(String name)
@WebServlet("/setSession")
public class SetSession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("name","pwd");
}
}
@WebServlet("/getSession")
public class GetSession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Object name = session.getAttribute("name");
System.out.println(name);
}
}
先在浏览器访问/setSession,再访问/get/Session
当访问//setSession时,由于是第一次访问,Request header is no parameters,But the response in the headset-Cookie


3.Session的特点
(1)session用于存储一次会话的多次请求的数据,Cookie存在服务器端
(2)session没有数据大小限制,Cookie有限制
3.1客户端关闭后,服务器不关闭,两次获取到的session不是同一个
@WebServlet("/demo")
public class Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println(session);
System.out.println("------------------");
}

如果需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存.
@WebServlet("/demo")
public class Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println(session);
System.out.println("------------------");
Cookie cookie = new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(10);
resp.addCookie(cookie);
}
}
3.2客户端不关闭,服务器关闭后,两次获取到的session不是同一个
In order to ensure the data is not lost with the following two measures
i:Session的钝化:在服务器正常关闭之前,将session对象序列化到硬盘上
ii:Session的活化:在服务器启动后,将Session文件转化为内存中的session对象即可
3.3Session的存活时间?
i:服务器关闭
ii:session对象调用invalidate();
iii:tomcat的web.xml中session默认失效时间(30分钟)过后;
边栏推荐
- 无题十四
- 2022.8.3
- 使用HBuilder离线本地打包ipa教程
- 基于 Kubernetes 的微服务项目整体设计与实现
- The technological achievements of Shanghai Konan were selected into the "2021 Shanghai Network Security Industry Innovation Research Achievement Catalog" by the Municipal Commission of Economy and Inf
- Two-table query average grouping in sql server
- 请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
- Creo 9.0 基准特征:基准点
- leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
- Happens-before rules for threads
猜你喜欢

Analysis and practice of antjian webshell dynamic encrypted connection

深度学习21天——卷积神经网络(CNN):天气识别(第5天)

Assembly language (8) x86 inline assembly

Advanced usage of C language

Why do I recommend using smart async?

为什么我推荐使用智能化async?

MySQL内部函数介绍

Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上

周报2022-8-4

seata源码解析:TM RM 客户端的初始化过程
随机推荐
js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)
15.1.1、md—md的基础语法,快速的写文本备忘录
微服务 技术栈
韦东山 数码相框 项目学习(六)tslib的移植
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
Creo 9.0 基准特征:基准轴
PAT Grade B-B1020 Mooncake(25)
tensorflow.keras无法引入layers
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
【Excel实战】--图表联动demo_001
百行代码发射红心,程序员何愁命不中女朋友!
Imitation SBUS fixed with serial data conversion
Analysis and practice of antjian webshell dynamic encrypted connection
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(下)
How to realize the short press and long press detection of the button?
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
Xcode 12 ld: symbol(s) not found for architecture armv64
正则表达式replaceFirst()方法具有什么功能呢?