当前位置:网站首页>cookie与Session
cookie与Session
2022-07-28 03:54:00 【程序三两行】
概述
用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话。
保持各个客户端自己的数据,每个用户在使用浏览器与服务器进行会话的过程中,不可避免各自会产生一些数据,程序要想办法为每个用户保存这些数据
cookie
1、创建cookie
Cookie cookie = Cookie(String name, String value) cookie只能存储非中文字符串
2、向客户端发送cookie
response.addCookie(cookie名称)
cookie会以响应头方式发送给客户端
3、携带cookie
请求头当中就能够看到cookie信息,访问服务器的任何资源,一般情况下都会把cookie带去过
4、Cookie默认存储时间
如果不设置持久化时间,cookie会存储在浏览器的内存中,浏览器关闭cookie信息销毁
设置Cookie在客户端的存储时间,cookie.setMaxAge(int seconds);,设置的时间为秒,如果设置持久化时间,cookie信息会被持久化到浏览器的磁盘文件里,过期会自动删除
5、设置Cookie的携带路径
默认情况下会在访问创建cookie的web资源相同的路径都携带cookie信息
http://localhost:9991/demo/indexServlet 在demo/indexServlet下创建cookie http://localhost:9991/demo/index.jsp 在demo下的index.jsp访问时会携带cookie http://localhost:9991/test/index.jsp 在demo下 不会携带cookie
设置携带路径cookie.setPath(String path);
cookie.setPath("/demo/index")//只有访问index才携带cookie信息
cookie.setPath("/demo")//只有访问指定工程都会携带cookie
cookie.setPath("/")//访问服务器下部署的所有工程都会携带cookie6、删除Cookie
使用同名同路径的持久化时间为0的cookie进行覆盖即可
7、服务器如何获取客户端携带的cookie
//获取所有的cookie
Cookie[] cookies = request.getCookies();
if(cookies != null){
//遍历所有的cookie
for (Cookie cookie:cookies){
String name = cookie.getName();
//判断名称是否自己存储的
if(name.equals("myCookie")){
String mycookie = cookie.getValue();
}
}
}8、应用案列
记住我

上次登录时间
第一次访问,告诉用户是第一次访问,获取当前的时间,并把它写到cookie当中,响应给浏览器,用户下次访问时,获取用户携带的cookie,把日期在浏览器当中显示,记录最新的cookie

session
1、概述
Session技术是将数据存储在服务器端的技术,会为每个客户端都创建一块内存空间存储客户的数据,客户端需要每次都携带一个标识ID去服务器中寻找属于自己的内存空间Session需要借助于Cookie存储客户的唯一性标识SESSIONID
2、获取Session对象
HttpSession session = request.getSession();,获得专属于当前会话的Session对象,如果服务器端没有该会话的Session对象,会创建一个新的Session返回,如果已经有了属于该会话的Session直接将已有的Session返回,本质就是根据SESSIONID判断该客户端是否在服务器上已经存在session了
3、向session中存取对象
Session对象和servletContext域,request域一样是一个域对象
void setAttribute(String var1, Object var2); Object getAttribute(String var1); void removeAttribute(String var1);
4、session的生命周期

边栏推荐
- input 上传文件并回显 FileReader并限制选择文件时的类型
- Selenium--WEB自动化测试工具
- Recursion and non recursion are used to calculate the nth Fibonacci number respectively
- How to solve MySQL deep paging problem
- Data mining-01
- [leetcode] 34. Find the first and last positions of elements in the sorted array
- 测试用例管理工具
- Greedy - 53. Maximum subarray sum
- LeetCode 0140. 单词拆分 II
- Protocols in swift
猜你喜欢

Leetcode skimming: dynamic programming 08 (segmentation and subsets)

C语言:不创建临时变量实现两数交换

一个仿win10蓝屏的404页面源码

test case management tool

Summary of static blog building tools

基于SSM实现在线租房系统

It's amazing. This article completely explains the service interface testing

高等数学(第七版)同济大学 习题3-5 个人解答

Dynamic planning - 62. Different paths

Leetcode brush question: dynamic planning 09 (weight of the last stone II)
随机推荐
Skillfully use stack backtracking to help you quickly locate problems
95后阿里P7晒出工资单:真的是狠狠扎心了...
[openvx] VX for basic use of objects_ pyramid
Appnium--APP自动化测试工具
Dynamic programming - 474. One and zero
Do you regret doing automated testing?
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (the last 8 questions)
Several ways of connecting upper computer and MES
Dynamic programming - 509. Fibonacci number
【OPENVX】对象基本使用之vx_lut
数据挖掘-01
Embedded development: tips and techniques -- the best practice of defensive programming with C
C语言:求一个整数存储在内存中的二进制中1的个数
[openvx] VX for basic use of objects_ matrix
Monotonic stack - 739. Daily temperature
C language: find the number of 1 in binary stored in memory as an integer
[wrong question]mocha and railgun
Common weak network testing tools
TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
Capacity expansion and reduction of RBD block storage device (VI)