当前位置:网站首页>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>
边栏推荐
- Openwrt directory structure
- 《微信小程序-基础篇》带你了解小程序的路由系统(二)
- Go learning record II (window)
- Project summary 1 (header file, switch, &, bit variables)
- Abnova abcb10 (human) recombinant protein specification
- dolphinscheduler 1.2.1 数据迁移到 dolphinscheduler 2.0.5方法及迁移后数据测试记录
- STL tutorial 3- exception mechanism
- Thesis reading_ Relation extraction_ CASREL
- 功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
- 工作5-8年的外包测试员,平均年薪有多少?
猜你喜欢

VGg Chinese herbal medicine identification

微信小程序;AI智能配音助手

How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!

Function declaration and call of 17 generator

Abnova PSMA bead solution

【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码

接收传来得文件并下载(简单用法)a标签

微信小程序:微信也可以发闪照了闪照制作生成微信小程序源码下载,自定义闪照时间

Parameter passing of 18 generator function

硬核,成为高素质测试人员:学会和产品沟通需求
随机推荐
Using editor How to handle MD uploading pictures?
Chrome debugging tips
Do280openshift command and troubleshooting -- common troubleshooting and chapter experiments
Abnova PSMA bead solution
teqc进行GNSS数据质量分析时生成的s文件介绍
Composer by installation laravel
微信小程序:老年祝福短视频
Apache atlas quick start
Laravel 8.4 routing problem. At the end is the cross reference table on the left side of the editor, which can be understood by Xiaobai
Dolphin scheduler 2.0.5 spark task test summary (source code optimization)
ApiPost接口测试的用法之------Post
Separate use of alertmanager alarms and use of Prometheus configuration alarm rules
A mvc5+easyui enterprise rapid development framework source code BS framework source code
Usage of API interface test ------ post
ICer技能02makefile脚本自跑vcs仿真
Shadertoy basic teaching 02. Drawing smiling faces
微信小程序:全新趣味测试
硬核,成为高素质测试人员:学会和产品沟通需求
Brief ideas and simple cases of JVM tuning - why do you need JVM tuning?
Abnova blood total nucleic acid purification kit protocol