当前位置:网站首页>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分钟)过后;
边栏推荐
猜你喜欢

Concurrent CAS

CPU的亲缘性affinity

dotnet OpenXML parsing PPT charts Getting started with area charts

2022.8.3

欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)

如何实现按键的短按、长按检测?

js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)

egg框架使用(一)

Creo 9.0 基准特征:基准轴

Overall design and implementation of Kubernetes-based microservice project
随机推荐
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Clothing Image Classification (Day 3)
2022.8.3
深度学习21天——卷积神经网络(CNN):天气识别(第5天)
CCVR eases heterogeneous federated learning based on classifier calibration
After Keil upgrades to AC6, what changes?
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
在colab里怎样读取google drive数据
Hundred lines of code launch red hearts, why programmers lose their girlfriends!
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
matcher中find,matches,lookingAt匹配字符串的不同之处说明
MySQL使用聚合函数可以不搭配GROUP BY分组吗?
放大器OPA855的噪声计算实例
Keil升级到AC6后,到底有哪些变化?
ffmpeg drawtext 添加文本水印
无题九
21 Days of Deep Learning - Convolutional Neural Networks (CNN): Weather Recognition (Day 5)
C语言-数组
链表中的数字相加----链表专题
开源一夏|OpenHarmony如何查询设备类型(eTS)