当前位置:网站首页>Cookie&Session学习
Cookie&Session学习
2022-04-21 16:38:00 【乔巴菌儿】
目录
Cookie
【概念】
客户端会话技术,将数据保存到客户端。
【快速入门】
1. 创建Cookie对象,绑定数据。 new Cookie(String name, String value)
2. 发送Cookie对象。 response.addCookie(Cookie cookie)
3. 获取Cookie,拿到数据。 Cookie[] request.getCookies()
//1.创建cookie对象 Cookie c = new Cookie("msg","hello"); //2.发送Cookie response.addCookie(c); //3.获取Cookie Cookie[] cs = request.getCookies(); //获取数据,遍历Cookies if (cs != null) { for (Cookie c : cs) { String name = c.getName(); String value = c.getValue(); System.out.println(name+","+value); } }
【实现原理】
基于响应头set-cookie和请求头cookie实现。
【cookie的细节】
1. 一次可以发送多个cookie:(可以创建多个Cookie对象,使用response调用多次addCookie方法发送cookie即可)
//一次性发送多个cookie //1.创建cookie对象 Cookie c1 = new Cookie("msg","hello"); Cookie c2 = new Cookie("happy","smile"); //2.发送Cookie response.addCookie(c1); response.addCookie(c2);2. cookie在浏览器中的保存时长:1)默认情况下,当浏览器关闭后,Cookie数据被销毁 2)持久化存储
//cookie在浏览器中保存多长时间? //1.创建cookie对象 Cookie c1 = new Cookie("msg","setMaxAge"); //设置cookie的存储时间 //c1.setMaxAge(30); //将cookie持久化到硬盘,30秒后自动删除cookie文件 //c1.setMaxAge(-1); //默认值,一方关闭即删除数据 c1.setMaxAge(0); //删除cookie信息 //2.发送Cookie response.addCookie(c1);3. cookie能不能存中文:
- 在tomcat 8 之前 cookie中不能直接存储中文数据(需要将中文数据转码---一般采用URL编码(%E3))
- 在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码解析
4.cookie共享问题:
//1.创建cookie对象 Cookie c1 = new Cookie("msg","你好"); //设置path,让当前服务器下部署的所有项目共享Cookie信息 c1.setPath("/"); //2.发送Cookie response.addCookie(c1);
5. Cookie的特点和作用:
- cookie存储数据在客户端浏览器
- 浏览器对于单个cookie 的大小有限制(4kb) 以及 对同一个域名下的总cookie数量也有限制(20个)
作用:
- cookie一般用于存出少量的不太敏感的数据
- 在不登录的情况下,完成服务器对客户端的身份识别
Session
【概念】
服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession。
【快速入门】
1. 获取HttpSession对象:
- HttpSession session = request.getSession();
2. 使用HttpSession对象:
- Object getAttribute(String name)
- void setAttribute(String name, Object value)
- void removeAttribute(String name)
//获取session HttpSession session = request.getSession(); //存储数据 session.setAttribute("msg","hello,session~"); //获取session HttpSession session = request.getSession(); //获取数据 Object msg = session.getAttribute("msg"); System.out.println(msg);
【原理】
Session的实现是依赖于Cookie的。
【细节】
1. 当客户端关闭后,服务器不关闭,两次获取session是否为同一个?(不是)
注:如果需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存
- Cookie c = new Cookie("JSESSIONID",session.getId());
- c.setMaxAge(60*60);
- response.addCookie(c);
//使用session共享数据 //获取session HttpSession session = request.getSession(); System.out.println(session); //期望客户端关闭之后,session也能相同 Cookie c = new Cookie("JSESSIONID",session.getId()); c.setMaxAge(60*60); response.addCookie(c);2. 客户端不关闭,服务器关闭后,两次获取的session是同一个吗?(不是同一个,但是要确保数据不丢失。tomcat自动完成以下工作)
- session的钝化:在服务器正常关闭之前,将session对象系列化到硬盘上
- session的活化:在服务器启动后,将session文件转化为内存中的session对象即可
3. session什么时候被销毁?1)服务器关闭 2)session对象调用invalidate() 3)session默认失效时间 30分钟)
【session的特点】
session用于存储一次会话的多次请求的数据,存在服务器端。
session可以存储任意类型,任意大小的数据。
【session与Cookie的区别】
1. session存储数据在服务器端,Cookie在客户端。
2. session没有数据大小限制,Cookie有。
3. session数据安全,Cookie相对于不安全。
版权声明
本文为[乔巴菌儿]所创,转载请带上原文链接,感谢
https://blog.csdn.net/EVILDOERyyds/article/details/124260919
边栏推荐
猜你喜欢

信号与系统2022春季作业-第九次作业

es6如何判断数组是否重复

Campus talking notes (5)

4.25解锁OpenHarmony技术日!年度盛会,即将揭幕!

SQL -- database operation (DDL, DML, DQL) + use the command to view the storage location of the current database (database version query)

2022 bone conduction earphone ranking, the best bone conduction earphone recommended

中国创投,凛冬将至

Changan dark blue's first product can be pure electricity, extended range and hydrogen electricity, with an acceleration of 5.9s

If this question doesn't work, the interviewer will continue to ask me the JVM: how to judge whether an object is recyclable

MinGW Download
随机推荐
打卡:4.21 C语言篇 -(1)初识C语言 - (11)关键字register,#define定义的宏
云呐:大型医疗设备资产管理系统贵吗?医院资产管理的主要内容
Arthas Tunnel使用
Find the longest substring of a string
How where used list in SAP GUI is implemented
Assembly language programming: design and debugging of input character type statistics in modular programming
怡宝IPO,农夫山泉慌了?
What are the advantages of mini led
2-4. 端口绑定
2022数二真题
Historical evolution, application and security requirements of the Internet of things
在线词典网站
SQL -- database operation (DDL, DML, DQL) + use the command to view the storage location of the current database (database version query)
The console displays VM + number + file name to debug
昊天旭辉签约长扬科技,携手共建工业互联网安全新生态
启动Redis的过程
Add release configuration in clion
程序设计天梯赛L3-28 森森旅游(想到multiset就算成功)
2022 number two real problem
[interview ordinary people vs Expert Series] can you talk about CAS mechanism?

