当前位置:网站首页>19.服务器端会话技术Session
19.服务器端会话技术Session
2022-08-05 09:16:00 【学习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时,由于是第一次访问,请求头是不带参数的,但是响应头中有set-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不是同一个
为了确保数据不丢失有了以下两种措施
i:Session的钝化:在服务器正常关闭之前,将session对象序列化到硬盘上
ii:Session的活化:在服务器启动后,将Session文件转化为内存中的session对象即可
3.3Session的存活时间?
i:服务器关闭
ii:session对象调用invalidate();
iii:tomcat的web.xml中session默认失效时间(30分钟)过后;
边栏推荐
- Assembly language (8) x86 inline assembly
- C语言-数组
- tensorflow.keras cannot introduce layers
- openpyxl操作Excel文件
- 16 kinds of fragrant rice recipes
- The difference between beautiful MM and ordinary MM
- 嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
- sql server收缩日志的作业和记录,失败就是因为和备份冲突了吗?
- 在colab里怎样读取google drive数据
- Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)
猜你喜欢

画法几何及工程制图考试卷A卷

2.4G无线收发模块的应用

Embedded practice ---- based on RT1170 transplant memtester to do SDRAM test (25)

使用HBuilder离线本地打包ipa教程

express hot-reload

【LeetCode】623. 在二叉树中增加一行

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

手把手教你纯c实现异常捕获try-catch组件

There is only one switch, how to realize the nqa of master-slave automatic switching

Redis源码解析:Redis Cluster
随机推荐
微信小程序请求封装
为什么我推荐使用智能化async?
shell脚本实例
16 kinds of fragrant rice recipes
Linux导出数据库数据到硬盘
无题四
在colab里怎样读取google drive数据
请问如果想往mysql里面写数据,直接用flink-connector-jdbc就可以吧,可是我在f
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
基于 Kubernetes 的微服务项目整体设计与实现
放大器OPA855的噪声计算实例
HStreamDB Newsletter 2022-07|分区模型优化、数据集成框架进一步完善
Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
按钮上显示值的轮流切换
How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
Assembly language (8) x86 inline assembly
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
Walk 100 trick society
使用 External Secrets Operator 安全管理 Kubernetes Secrets
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)