当前位置:网站首页>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分钟)过后;
边栏推荐
- C语言的高级用法
- Creo 9.0 基准特征:基准平面
- Two-table query average grouping in sql server
- express hot-reload
- PAT乙级-B1019 数字黑洞(20)
- js graphics operation one (compatible with pc, mobile terminal to achieve draggable attribute drag and drop effect)
- sql server中 两表查询 平均数 分组
- PAT乙级-B1020 月饼(25)
- How to realize the short press and long press detection of the button?
- CPU的亲缘性affinity
猜你喜欢
随机推荐
egg框架使用(一)
PAT Level B - B1021 Single Digit Statistics (15)
dotnet OpenXML 解析 PPT 图表 面积图入门
How to realize the short press and long press detection of the button?
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
汇编语言(8)x86内联汇编
韦东山 数码相框 项目学习(六)tslib的移植
Two-table query average grouping in sql server
明天去订票,准备回家咯~~
2022/8/4 考试总结
正则表达式replaceAll()方法具有什么功能呢?
在colab里怎样读取google drive数据
上海控安技术成果入选市经信委《2021年上海市网络安全产业创新攻关成果目录》
自定义过滤器和拦截器实现ThreadLocal线程封闭
无题七
C语言的高级用法
Creo 9.0 基准特征:基准点
mysql进阶(二十七)数据库索引原理
使用稀疏 4D 卷积对 3D LiDAR 数据中的运动对象进行后退分割(IROS 2022)
js 图形操作一(兼容pc、移动端实现 draggable属性 拖放效果)









