当前位置:网站首页>会话技术相关
会话技术相关
2022-07-24 03:41:00 【endorphinnnn】
会话跟踪
会话:用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,会话结束。一次会话中可以包含多次请求和响应。
会话跟踪:一种维护浏览器状态的方法,服务器需要识别多次请求是否来自同一浏览器,以便在同一次会话的多次请求间共享数据。
如何实现会话跟踪技术?
1)客户端会话跟踪技术:Cookie
2)服务端会话跟踪技术:Session
区别:前者存储在浏览器端后者存储在服务器端
Cookie
概念
客户端会话技术,将数据保存到客户端,以后每次请求都携带Cookie数据进行访问。
Cookie基本使用
发送Cookie
- 创建Cookie对象,并设置数据:
Cookie cookie = new Cookie(“key”,“value”); - 发送Cookie端到客户端
response.addCookie(cookie)
获取Cookie
- 获取客户端携带的所有Cookie,使用request对象
Cookie[] cookie = request.getCookies(); - 使用Cookie对象方法获取数据:
cookie.getName();cookie.getValue();
Cookie的存活时间和如何存储中文
- 默认情况下,Cookie存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁。
- 设置Cookie存活时间:setMaxAge(int seconds)
1.正数:将Cookie写入浏览器所在电脑的硬盘,持久化存储。到时间自动删除
2.负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,则Cookie被销毁
3.零:删除对应Cookie - 中文的编码解码
编码:URLEncoder.encode(string,“utf-8”)
解码:URLDecoder.decode(string,“utf-8”)
Session
概念
服务端会话跟踪技术,将数据保存到服务端
Session的基本使用
javaEE中提供了HttpSession接口,来实现一次会话的多次请求之间数据共享功能。
- 获取Session对象,使用的是request对象
HttpSession session = request.getSession(); - Session对象提供的功能:
1、存储数据到session域中:void setAttribute(String name,Object o)
2、根据key,获取值:object getAttribute(String name)
3、根据key,删除该键值对:void removeAttribute(String name)
Session中可以存储任意数据类型
Session的钝化和活化
钝化:在服务器正常关闭后,Tomcat会自动将Session数据写入硬盘的文件中
活化:再次启动服务器后,从文件中加载数据到Session中
session数据不能长期保存数据,cookie是存储在客户端
Session销毁
- 默认情况下,无操作,30分钟自动销毁
- 调用Session对象的invalidate()进行销毁,销毁方法一般会在用户退出的时候,需要将session销毁掉
边栏推荐
- An in-depth explanation of CAS is necessary for interview practice
- Jump statement in day011 loop structure
- STL set container
- Arduino interrupt realizes rising edge detection and executes other functions
- 【无标题】
- Conteneur STL set
- 数据湖:Apache Hudi简介
- STL set容器
- 4. Hezhou air32f103_ LCD
- Bet on the whole scene, what is the odds of glory?
猜你喜欢

CVE-2022-29464 WSO2文件上传漏洞

What is IMU?

MySql的DDL和DML和DQL的基本语法

JIRA automation experience sharing for 2 years

Gpushare. COM | how to use tensorboardx visualization tool?

Leetcode-382. random nodes of linked list

08 reptile project

Lagrange polynomial

uniapp H5打包后本地图片无法显示问题

MySQL learning - MySQL software installation and environment configuration (Windows) details!
随机推荐
The error of van swipe in the rotation chart: cannot read a properties of null (reading width)
Lagrange polynomial
Machine learning notes - image homography estimation based on deep learning (homographynet)
What is IMU?
Error code 0x80004005
Matlab ode45 solving differential equations
Redis transaction learning
Matlab Simulink hydropower and synchronous motor power generation
STL set容器
Read and understand the advantages of the LAAS scheme of elephant swap
08 reptile project
IO流分类整理
Introduction to pytorch ecology
Android Development - lambda expression of kotlin syntax
Database foundation and installation
Using global data to realize data sharing in wechat applet
Ue5 split screen (small map) solution
What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
【无标题】
正则表达式 \b \B 深入浅出理解单词边界的匹配