当前位置:网站首页>Cookie-Session讲解
Cookie-Session讲解
2022-06-23 03:53:00 【不想当个程序员】
1、会话
会话:用户打开一个浏览器,点击了很多超链接,访问多了web资源,关闭浏览器,这个过程可以称之为会话。
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过教室,称之为有状态会话。
你怎么证明你是一个学生?
- 学生证:学校给你学生证
- 学校登记:学校标记你来过
一个网站怎么证明你来过?
客户端 服务端
- 服务端给客户端一个信件,客户端下次访问服务器带上信件就可以了;cookie
- 服务器登记你来过,下次来的时候来匹配你;session
2、保存会话的两种技术
cookie:客户端技术(响应、请求)
session:服务器技术,利用这个技术,可以保存用户的会话信息?我们可以把信息或者数据放在Session中!
常见常例:网站登录之后,下次不用在登陆了,第二次访问就直接能上去了。
3、Cookie
- 从请求中拿到cookie信息
- 服务器相应给客户端cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 服务器告诉你,你来的时间,把这个时间封装成一个信件,你下次带来 我就知道你来了
req.setCharacterEncoding("gbk");
resp.setCharacterEncoding("gbk");
PrintWriter out = resp.getWriter();
// Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
// 判断Cookie是否存在
if (cookies!=null){
// 如果存在怎么办
out.write("你上一次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
// 获取cookie的名字
if (cookie.getName().equals("lastLoginTime")){
// 获取cookie中的值
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else {
out.write("这是第一次访问本站");
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
cookie.setMaxAge(24*60*60);//有效期为一天
resp.addCookie(cookie);
}
req.getCookies();//获取cookie
cookie.getName();//获取cookie的key
cookie.getValue();//获取cookie中的value
new Cookie("lastLoginTime", System.currentTimeMillis()+"");//新建一个cookie
cookie.setMaxAge(**);// 设置cookie有效期
resp.addCookie();//响应给客户端一个cookie
Cookie:一般会保存在本地用户目录下appdata;
一个网站cookie是否存在上限!聊聊细节问题
- 一个Cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
- Cookie大小有限制4kb;
- 300个cookie浏览器上限
删除cookie;
- 不设置有效期,关闭浏览器,自动失效;
- 设置有效期时间为0;
// 创建一个cookie,名字必须要和要删除的一样
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
// cookie有效期设置为0,立马过期
cookie.setMaxAge(0);
resp.addCookie(cookie);
编码解码:
URLDecoder.decode(cookie.getValue(),"UTF-8");
URLEncoder.encode("中文Cookie","utf-8");
4、Session(重点)
什么是Session:
- 服务器会给每一个用户(浏览器)创建一个Session对象,
- 一个Session独占一个浏览器,只要浏览器没关,这个Session就存在
- 用户登录之后,整个网站它都可以访问!–保存用户的信息;保存购物车的信息……
session和Cookie的区别:
- cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
- Session把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
- session对象由服务创建;
使用场景:
- 保存一个登录用户的信息;
- 购物车信息;
- 在整个网站中经常会使用的数据,我们将它保存在Session中
手动注销
// 手动注销session
session.invalidate();
会话自动过期
<!-- 设置session默认的失效时间-->
<session-config>
<!-- 1分钟后session自动失效-->
<session-timeout>1</session-timeout>
</session-config>
边栏推荐
- Shadertoy basic teaching 02. Drawing smiling faces
- Pads and flash symbols in cadence
- dolphinscheduler海豚调度升级代码改造-UpgradeDolphinScheduler
- E45: ‘readonly‘ option is set (add ! to override)
- 云原生数据库如荼如火,未来可期
- JDBC调用存储过程、MySQL触发器
- 强推,软件测试快速入门,一看就会
- Summary of switched reluctance motor suspension drive ir2128
- ICer技能03Design Compile
- 【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码
猜你喜欢

② Cocoapods principle and podspec file uploading operation

PCB -- bridge between theory and reality

Course design C for freshmen -- clothing management system
![[graph theory] - bipartite graph](/img/2d/999820edafe7294440cf1873a26084.png)
[graph theory] - bipartite graph

Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection

2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。 package main import ( “fmt“ ) func mai

物体结构图,快速图解物体内部结构

dolphinscheduler 1.2.1 数据迁移到 dolphinscheduler 2.0.5方法及迁移后数据测试记录

美团好文:从预编译的角度理解Swift与Objective-C及混编机制

Abnova fluorescent dye 555-c3 maleimide scheme
随机推荐
TabControl style of WPF basic control
AlertManager告警的单独使用及prometheus配置告警规则使用
Decompile
如何更好地组织最小 WEB API 代码结构
Can bus Basics
Abnova fluorescent dye 555-c3 maleimide scheme
云原生数据库如荼如火,未来可期
Course design C for freshmen -- clothing management system
Thesis reading_ Relation extraction_ CASREL
The solution to prompt "this list creation could be rewritten as a list literal" when adding elements to the list using the append() method in pychart
Metadata management Apache Atlas Compilation (embedded) deployment and various error records encountered
Seven year manong Road
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
Arduino温湿度传感器DHT11(含代码)
Mini Homer - can you get a remote map data transmission link for hundreds of yuan?
物体结构图,快速图解物体内部结构
Li Kou today's question 513 Find the value in the lower left corner of the tree
Using editor How to handle MD uploading pictures?
Three operation directions of integral mall
《微信小程序-基础篇》带你了解小程序的路由系统(二)