当前位置:网站首页>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>
边栏推荐
猜你喜欢

GNSS速度解算的三种方法

Do280openshift command and troubleshooting -- common troubleshooting and chapter experiments

How can mushrooms survive a five-year loss of 4.2 billion yuan?

Mini Homer——几百块钱也能搞到一台远距离图数传链路?

ApiPost接口测试的用法之------Post

Meituan's good article: understand swift, Objective-C and the mixing mechanism from the perspective of precompiling

ICER skills 03design compile

超越芯片和AI,硬科技资本为什么越来越“硬核”?

rtklib新版本2.4.3 b34测试对比

With the arrival of intelligent voice era, who is defining AI in the new era?
随机推荐
centos7部署docker,安装mysql
Metadata management Apache Atlas Compilation (embedded) deployment and various error records encountered
Dsp7 environment
【图像融合】基于非凸罚分的稀疏正则化实现图像融合附matlab代码
What are the types of independent station chat robots? How to quickly create your own free chat robot? It only takes 3 seconds!
MySQL存储过程
Abnova abcb10 (human) recombinant protein specification
How to use data to tell a wonderful story?
Cloud function realizes fuzzy search function
Go learning record II (window)
Separate use of alertmanager alarms and use of Prometheus configuration alarm rules
微信小程序:老年祝福短视频
prometheus、influxdb2.2安装及flume_export下载编译使用
GNSS速度解算的三种方法
Pads and flash symbols in cadence
Arduino火焰传感器(含代码)
Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection
Shadertoy基础教学01、画圆(smoothstep()函数讲解)
396. mine site construction
Install and run mongodb under win10