当前位置:网站首页>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的生命周期

边栏推荐
- 4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits
- 数据挖掘-01
- Developing rc522 module based on c8t6 chip to realize breathing lamp
- Input upload file and echo FileReader and restrict the type of file selection
- Capacity expansion and reduction of RBD block storage device (VI)
- Leetcode 0140. word splitting II
- [P4] check the differences between the two historical versions of the library file
- Differences among BRD, MRD and PRD
- Recursion and non recursion are used to calculate the nth Fibonacci number respectively
- Practical scripts of mangopapa (contents)
猜你喜欢

接口自动化测试,完整入门篇

Protocols in swift

Analysis of static broadcast transmission process

Detailed explanation of string + memory function (C language)

Simple and easy-to-use performance testing tools recommended

Weekly recommended short video: how to correctly understand the word "lean"?

Notes on writing questions in sword finger offer

Appnium -- app automated test tool

Common weak network testing tools

Developing rc522 module based on c8t6 chip to realize breathing lamp
随机推荐
Embedded development: tips and techniques -- the best practice of defensive programming with C
Crowdfunding platform system based on JSP & Servlet
Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
Leetcode skimming: dynamic programming 08 (segmentation and subsets)
Dynamic planning - 62. Different paths
Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
8000 word explanation of OBSA principle and application practice
【OPENVX】对象基本使用之vx_lut
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
Redis cluster
In the official online CV2 document, check the optional values of OpenCV specific parameters
Leetcode 0140. word splitting II
Leetcode 0141. circular linked list - three solutions
《剑指offer》| 刷题小记
[image classification] 2021 MLP mixer nips
Dynamic planning - 63. Different paths II
【P4】 查看库文件两个历史版本的区别
Summary and interpretation of CONDA virtual environment
Data mining-02