当前位置:网站首页>Session tracking technology cookies and sessions
Session tracking technology cookies and sessions
2022-07-17 23:04:00 【Bai Xiaoyun】
Session tracking technology
A method of maintaining browser state , The server needs to identify whether multiple requests come from the same browser , To share data between multiple requests in the same session
Cookie:
Client session technology : Save data to client , Every request in the future will carry Cookie Data access
Cookie Basic use
send out Cookie:
1. establish Cookie object , Set up the data
Cookie cookie=new Cookie("key","value");2. send out Cookie To client : Use response object
response.addCookie(cookie)obtain Cookie
1. obtain Cookie Array
Cookie[] cookies = request.getCookies();2. Traversal array
for (Cookie c :cookies
) {
String name = c.getName();
if ("username".equals(name)) {
//3. obtain Cookie data
String value = c.getValue();
System.out.println(name+":"+value);
}
}
Cookie principle
Cookie The implementation is based on HTTP Agreed
Response head :set-cookie
Request header :cookie
Cookie Details of the use of :
Cookie Life time of :
By default ,Cookie Stored in browser memory , When the browser closes , Memory free , be Cookie Be destroyed
setMaxAge(int seconds): Set up Cookie Life time of
1. Positive numbers : take Cookie Write to the computer hard disk where the browser is located , Persistent storage . Automatically delete by time
2. negative : The default value is ,Cookie In the current browser memory , When the browser closes , be Cookie Be destroyed
3. zero : Delete corresponding Cookie
@WebServlet("/asServlet")
public class asServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// send out Cookie
//1. establish Cookuie object
Cookie cookie=new Cookie("username","zhangsan");
// Set the time to live 7 God
cookie.setMaxAge(60*60*24*7);
//2. send out Cookie
response.addCookie(cookie);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}Cookie Store Chinese :
//URL code
String value=" Zhang San ";
value= URLEncoder.encode(value,"UTF-8");
Cookie cookie=new Cookie("username",value); ![]()
String value = c.getValue();
//URL decode
value= URLDecoder.decode(value);
System.out.println(name+":"+value); ![]()
Session
Server session tracking technology : Save the data to the server
Session Is based on Cookie Realized
Use :
session.setAttribute// Storage
session.getAttribute// obtain
session.removeAttribute// Delete
//1. obtain Session object
HttpSession session = request.getSession();
//2. Storage Session object
session.setAttribute("username","zhangsan");//1. obtain Session object
HttpSession session = request.getSession();
// get data
Object username = session.getAttribute("username");
System.out.println("username:"+username);Session Details of the use of :
Session passivation , activation :
After the server restarts ,Session The data in also exists
passivation : After the server shuts down normally ,Tomcat Will automatically Session Write to the file on the hard disk
activation : After starting the server again , Load data from file to Session in
Session The destruction :
1. stay web.xml Middle configuration
Default 30 Minute destruction
<session-config>
<session-timeout>100</session-timeout>
</session-config>2. Destroy yourself
session.invalidate();summary :
Cookie and Session It is to complete the data sharing between multiple requests in a session
difference :
Storage location :Cookie Is to store data to the client ,Session Is to store data to the server
Security :Cookie unsafe ,Session Security
data size :Cookie Maximum 3KB,Session No size limit
Storage time :Cookie Long term storage ,Session Default 30 minute
Server performance :Cookie Does not occupy server resources ,Session Occupancy resources
边栏推荐
- Leetcode exercise - Sword finger offer 32 - I. print binary tree from top to bottom
- ES6中Array对象的方法和扩展、string的扩展方法、数组的遍历。(含例题)
- c语言基础篇:操作符
- Proxmox VE 7.2 Install SMB 服务
- AI opencvsharp big picture to small picture (case version)
- Communication mode - FSMC
- [pypdf2] merge PDF, rotate, zoom, crop, encrypt and decrypt, add watermark
- Arkui FAQ summary [series 3]
- Complex entanglement between pointer and array
- 2022年了,我该如何入门智能家居
猜你喜欢
随机推荐
陆面生态水文模拟与多源遥感数据同化与Noah-MP模型
[MySQL] MySQL groups statistical data by year / month / day / week
What constitutes an Oracle tablespace?
AI opencvsharp big picture to small picture (case version)
Apache Flink 在移动云实时计算的实践
Typescript 13 starting from 0: infer, extends, keyof, typeof, in
ArkUI常见问题汇总【系列3】
Government organizations improve the efficiency, transparency and control of information management through content management
Flink 在 B 站的多元化探索与实践
Vs prompt when opening the project: when loading the file with Unicode (UTF-8) encoding * * *, some bytes have been replaced with Unicode replacement characters.
Wechat applet - get element positioning and width in applet custom components
面向流批一体的 Flink Runtime 新进展
What architecture does polardb for PostgreSQL adopt?
Maatnesite/excel import and export in laravel
Land surface eco hydrological simulation and multi-source remote sensing data assimilation and Noah MP model
Arkui FAQ summary [series 3]
【重识云原生】第四章云网络4.9.4.1节——智能网卡SmartNIC方案综述
[solution] the solution to the unresponsive keys under the old unity inputsystem
Item of the scrapy component
[interview: concurrent Article 13: multithreading: thread safety analysis of variables]









