当前位置:网站首页>Cookie & session learning
Cookie & session learning
2022-04-22 01:01:00 【Jojoba】
Catalog
Cookie
【 Concept 】
Client session technology , Save data to client .
【 Quick start 】
1. establish Cookie object , Data binding . new Cookie(String name, String value)
2. send out Cookie object . response.addCookie(Cookie cookie)
3. obtain Cookie, Get the data . Cookie[] request.getCookies()
//1. establish cookie object Cookie c = new Cookie("msg","hello"); //2. send out Cookie response.addCookie(c); //3. obtain Cookie Cookie[] cs = request.getCookies(); // get data , Traverse Cookies if (cs != null) { for (Cookie c : cs) { String name = c.getName(); String value = c.getValue(); System.out.println(name+","+value); } }
【 Realization principle 】
Based on the response header set-cookie And the request header cookie Realization .
【cookie The details of the 】
1. You can send more than one at a time cookie:( You can create multiple Cookie object , Use response Call several times addCookie Method to send cookie that will do )
// Send multiple... At a time cookie //1. establish cookie object Cookie c1 = new Cookie("msg","hello"); Cookie c2 = new Cookie("happy","smile"); //2. send out Cookie response.addCookie(c1); response.addCookie(c2);2. cookie Save time in browser :1) By default , When the browser is closed ,Cookie The data is destroyed 2) Persistent storage
//cookie How long to save in the browser ? //1. establish cookie object Cookie c1 = new Cookie("msg","setMaxAge"); // Set up cookie Storage time of //c1.setMaxAge(30); // take cookie Persist to hard disk ,30 Automatically delete... In seconds cookie file //c1.setMaxAge(-1); // The default value is , Delete data when one party closes c1.setMaxAge(0); // Delete cookie Information //2. send out Cookie response.addCookie(c1);3. cookie Can you save Chinese :
- stay tomcat 8 Before cookie Can't store Chinese data directly in ( Need to transcode Chinese data --- It is generally used URL code (%E3))
- stay tomcat 8 after ,cookie Support Chinese data . Special characters are still not supported , It is recommended to use URL Encoding storage ,URL Decoding and parsing
4.cookie Sharing issues :
//1. establish cookie object Cookie c1 = new Cookie("msg"," Hello "); // Set up path, Let all projects deployed under the current server share Cookie Information c1.setPath("/"); //2. send out Cookie response.addCookie(c1);
5. Cookie The characteristics and functions of :
- cookie Store data in the client browser
- Browser for single cookie There is a limit to the size of (4kb) as well as For the total under the same domain name cookie There is also a limit to the number (20 individual )
effect :
- cookie Generally used to store a small amount of less sensitive data
- Without logging in , Complete the identification of the client by the server
Session
【 Concept 】
Server side session technology , Sharing data among multiple requests in a session , Save the data in the object on the server side .HttpSession.
【 Quick start 】
1. obtain HttpSession object :
- HttpSession session = request.getSession();
2. Use HttpSession object :
- Object getAttribute(String name)
- void setAttribute(String name, Object value)
- void removeAttribute(String name)
// obtain session HttpSession session = request.getSession(); // Store the data session.setAttribute("msg","hello,session~"); // obtain session HttpSession session = request.getSession(); // get data Object msg = session.getAttribute("msg"); System.out.println(msg);
【 principle 】
Session The realization of depends on Cookie Of .
【 details 】
1. When the client is shut down , The server does not shut down , Get twice session Is it the same ?( No )
notes : If you need the same , You can create Cookie, The key is JSESSIONID, Set the maximum lifetime , Give Way cookie Persistent save
- Cookie c = new Cookie("JSESSIONID",session.getId());
- c.setMaxAge(60*60);
- response.addCookie(c);
// Use session Shared data // obtain session HttpSession session = request.getSession(); System.out.println(session); // After the client is expected to shut down ,session It can be the same Cookie c = new Cookie("JSESSIONID",session.getId()); c.setMaxAge(60*60); response.addCookie(c);2. The client does not shut down , After the server is shut down , Acquired twice session Is it the same ?( Not the same , But make sure the data is not lost .tomcat Automatically complete the following work )
- session Passivation of : Before the server is shut down properly , take session Serialize objects to hard disk
- session Activation of : After the server starts , take session The file is converted to... In memory session Object can
3. session When it was destroyed ?1) Server down 2)session Object call invalidate() 3)session Default expiration time 30 minute )
【session Characteristics 】
session Data used to store multiple requests for a session , There is a server side .
session Can store any type , Data of any size .
【session And Cookie The difference between 】
1. session Store data on the server side ,Cookie On the client side .
2. session There is no data size limit ,Cookie Yes .
3. session Data security ,Cookie Relative to insecurity .
版权声明
本文为[Jojoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211638089648.html
边栏推荐
- 星环科技基础软件产品全面落地开花,为企业数字化转型带来“星”动能
- 诚邀报名丨首期OpenHarmony开发者成长计划分享日
- js数组对象去重
- 终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享
- 程序员怎么个人接单?
- Fundamentals of digital electronic technology 3.3 CMOS gate circuit (Part 2)
- Serialization and transient keywords
- Rasa对话机器人连载一 第121课:Rasa对话机器人Debugging项目实战之电商零售对话机器人运行流程调试全程演示-1
- [knowledge atlas] tushare data acquisition and display (1 / *)
- 阿里超大规模 Flink 集群运维体系介绍
猜你喜欢

MDC manages multithreaded logs

诚邀报名丨首期OpenHarmony开发者成长计划分享日
![[redis] using redis to optimize the display of provinces, the data will not be displayed](/img/cd/512223b6ecb06d0cd60510df600ca0.png)
[redis] using redis to optimize the display of provinces, the data will not be displayed

小米和智汀的智能摄像头,保护你的家庭隐私

智能名片小程序创建名片页功能实现关键代码

Several ways of pytoch data encapsulation before entering the network

R-Dropout

GPU的未来方向是什么?

Discrete mathematical propositional logic

Smart business card applet creates business card page function and realizes key code
随机推荐
I use ehcache to improve the query performance by 100 times. It's really fragrant
星环科技基础软件产品全面落地开花,为企业数字化转型带来“星”动能
【服务器数据恢复】服务器RAID6多个硬盘先后离线的数据恢复案例
2022 superstar learning link: media creative economy, entering ASEAN, career promotion, music appreciation, emergencies and self rescue
从概念到实现,智能家居如何逐步走进我们的生活
What do you need to know to join smart home?
R语言广义线性模型GLM:线性最小二乘、对数变换、泊松、二项式逻辑回归分析冰淇淋销售时间序列数据和模拟
Ideal one finally replaced? Brand new product sequence named L8, heavily camouflaged road test exposure
hex,base64,urlencode编码方案对比
观测云登陆阿里云计算巢,共建ISV新生态
kubernetes集群之Pod说能不能让我体面的消亡呀?
Solve the problem that prettier is invalid after svelte project uses pnpm
Material UI中JSS的写法(随手笔记)
实现Nest中参数的联合类型校验
From concept to realization, how does smart home come into our life step by step
【课程汇总】Hello HarmonyOS系列课程,手把手带你零基础入门
[langage C] opération de fichier d'analyse approfondie [niveau avancé examen spécial]
How to select Bi tools? These three questions are the key!
How to set the validity period of win11 account password? Win11 account password usage period setting tutorial
Relevant changes in training and verification after the combination of variable and tensor

