当前位置:网站首页>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分钟)过后;
边栏推荐
- 全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
- 随时牵手 不要随意分手[转帖]
- The Secrets of the Six-Year Team Leader | The Eight Most Important Soft Skills of Programmers
- 2022-08-01 Review the basic binary tree and operations
- ECCV 2022 Oral 视频实例分割新SOTA:SeqFormer&IDOL及CVPR 2022 视频实例分割竞赛冠军方案...
- 【LeetCode】623. Add a row to the binary tree
- 在colab里怎样读取google drive数据
- Xcode 12 ld: symbol(s) not found for architecture armv64
- Linux导出数据库数据到硬盘
- 【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
猜你喜欢
随机推荐
接口全周期的生产力利器Apifox
Creo 9.0 基准特征:基准轴
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
Luogu P4588: [TJOI2018]数学计算
There is only one switch, how to realize the nqa of master-slave automatic switching
工程制图知识点
代码审计—PHP
程序员的七种武器
动态库之间回调函数使用
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
放大器OPA855的噪声计算实例
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
让程序员崩溃的N个瞬间(非程序员误入)
Seata source code analysis: initialization process of TM RM client
tensorflow.keras cannot introduce layers
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
PAT Level B - B1021 Single Digit Statistics (15)
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
如何实现按键的短按、长按检测?
Example of Noise Calculation for Amplifier OPA855









