当前位置:网站首页>Cookie session explanation
Cookie session explanation
2022-06-23 05:08:00 【Don't want to be a programmer】
1、 conversation
conversation : The user opens a browser , Click a lot of hyperlinks , More visits web resources , Close the browser , This process can be called conversation .
Stateful conversation : A classmate came to the classroom , Come back to the classroom next time , We will know this classmate , I have been to the classroom , It's called stateful conversation .
How can you prove that you are a student ?
- Student ID card : The school will give you a student card
- School Registration : The school signs you've been here
How can a website prove that you've been here ?
client Server side
- The server sends a letter to the client , The next time the client accesses the server, it is OK to bring a letter ;cookie
- The server registers that you have been here , Next time I come, I will match you ;session
2、 Two techniques for saving a session
cookie: Client technology ( Respond to 、 request )
session: Server technology , Using this technology , Can save user session information ? We can put information or data in Session in !
Common practice : After the website logs in , Don't log in next time , On the second visit, you can go up directly .
3、Cookie
- Get... From the request cookie Information
- The server corresponding to the client cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// The server tells you , When you came , Encapsulate this time into a letter , You bring it next time I knew you were here
req.setCharacterEncoding("gbk");
resp.setCharacterEncoding("gbk");
PrintWriter out = resp.getWriter();
// Cookie, The server gets it from the client
Cookie[] cookies = req.getCookies();// Here we return the array , explain Cookie There could be multiple
// Judge Cookie Whether there is
if (cookies!=null){
// What if it exists
out.write(" The time of your last visit was :");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
// obtain cookie Name
if (cookie.getName().equals("lastLoginTime")){
// obtain cookie The value in
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else {
out.write(" This is my first visit to this site ");
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
cookie.setMaxAge(24*60*60);// Valid for one day
resp.addCookie(cookie);
}
req.getCookies();// obtain cookie
cookie.getName();// obtain cookie Of key
cookie.getValue();// obtain cookie Medium value
new Cookie("lastLoginTime", System.currentTimeMillis()+"");// Create a new one cookie
cookie.setMaxAge(**);// Set up cookie The period of validity
resp.addCookie();// The response gives the client a cookie
Cookie: It is usually saved in the local user directory appdata;
A website cookie Whether there is an upper limit ! Talk about the details
- One Cookie Only one message can be saved
- One web Sites can send multiple cookie, At most 20 individual cookie;
- Cookie There's a limit to the size 4kb;
- 300 individual cookie Browser limit
Delete cookie;
- Do not set the validity period , Close the browser , Automatic failure ;
- Set the validity period to 0;
// Create a cookie, The name must be the same as the one to be deleted
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
// cookie The validity period is set to 0, Expire immediately
cookie.setMaxAge(0);
resp.addCookie(cookie);
Encoding and decoding :
URLDecoder.decode(cookie.getValue(),"UTF-8");
URLEncoder.encode(" chinese Cookie","utf-8");
4、Session( a key )
What is? Session:
- The server will give every user ( browser ) Create a Session object ,
- One Session Monopolize a browser , As long as the browser is not turned off , This Session There is a
- After the user logs in , It can access the whole website !– Save user information ; Save shopping cart information ……
session and Cookie The difference between :
- cookie It is to write the user's data to the user's browser , Browser save ( You can save multiple )
- Session Write the user's data to the user's exclusive Session in , Server side save ( Keep important information , Reduce the waste of server resources )
- session Objects are created by services ;
Use scenarios :
- Save the information of a login user ;
- Shopping cart information ;
- Data often used throughout the website , We keep it in Session in
Manual logout
// Manual logout session
session.invalidate();
Session expires automatically
<!-- Set up session Default expiration time -->
<session-config>
<!-- 1 Minutes later session Automatic failure -->
<session-timeout>1</session-timeout>
</session-config>
边栏推荐
- 功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
- go学习记录二(Window)
- UI自动化定位利器-xpath实战
- ZK has a node reporting it is possible not running and the log has no obvious error
- Icer Skill 02makefile script Running VCS Simulation
- Apache atlas quick start
- 微信小程序:微信也可以发闪照了闪照制作生成微信小程序源码下载,自定义闪照时间
- Less than a year after development, I dared to ask for 20k in the interview, but I didn't even want to give 8K after the interview~
- Small problems in the spoole framework for TCP communication in PHP
- 美团好文:从预编译的角度理解Swift与Objective-C及混编机制
猜你喜欢

2020:VL-BERT: Pre-training of generic visual-linguistic representation

Thesis reading_ Relation extraction_ CASREL

rtklib新版本2.4.3 b34测试对比

What are the types of independent station chat robots? How to quickly create your own free chat robot? It only takes 3 seconds!

ICer技能02makefile脚本自跑vcs仿真

UI自动化定位利器-xpath实战

Usage of apipost interface test ------ get

聊聊 C# 中的 Composite 模式

rtklib2.4.3 b34 单点定位的一个bug

985测试工程师被吊打,学历和经验到底谁更重要?
随机推荐
微信小程序;AI智能配音助手
How to use data to tell a wonderful story?
Cve-2019-14287 (sudo right raising)
ICer技能02makefile脚本自跑vcs仿真
[paper reading] semi supervised learning with ladder networks
开发一年不到,来面试居然敢开口要20K,面完连8K都不想给~
The solution to prompt "this list creation could be rewritten as a list literal" when adding elements to the list using the append() method in pychart
精密星历介绍与使用
395. redundant path
Three operation directions of integral mall
Seven year manong Road
Abnova abcb10 (human) recombinant protein specification
VGg Chinese herbal medicine identification
入行软件测试5年,跳槽3次,我摸透了软件测试这一行
Talk about the composite pattern in C #
MySQL存储过程
七年码农路
Shadertoy basic teaching 02. Drawing smiling faces
【Mac】安全性与隐私中没有任何来源选项
新晋职场人的 技术进击?之旅