当前位置:网站首页>cookie and session
cookie and session
2022-07-25 11:37:00 【Dream】
javaweb And :Cookie Mechanism
1. Study Cookie and Session Why ?
1.Http The agreement itself is stateless , namely No status information will be saved between multiple requests . But in actual development , Many times, you need to pass information between multiple requests . You need to learn the technology of maintaining conversation .cookie and session Are used to maintain conversation Technology .
2.Cookie Mechanism ?
javax.servlet.http.Cookie: class The only one JAVAWEB We can new
1.Cookie: Cookies , Little cookie . It is a small amount of string information passed by the server to the client . Save in the client browser ( Memory / Hard disk ), When you access the server again later , hold Cookie The information is sent back to the server .
Cookie Only text messages . No more than 4K
2. How to send cookie:[Server--->Client]
1.Cookie Information through response.addCookie(new Cookie(key,value)), The bottom layer of this method is by adding HTTP Respond to the header in a way that cookie Information is transmitted from the server to the client .
3. How to return cookie:[Client===>Server]
When accessing the server again ,Cookie Save information to HTTP Request header , Send back to the server . We're on the server (Servlet/Controller etc. .) adopt request.getCookies() Get the last server sent client carried by this request Cookie Information .
4.Cookie Is the only one we can new The object of , We must new, You can set the expiration time setMaxAge( Seconds per unit ), If not set maxage, Is saved in memory .
3. Case study :
1. Remember me Remember the user name
2. Last visit
3. Recently viewed products
Session Mechanism
1.Session The principle of mechanism
0.session To express a conversation . In general, a session contains multiple requests .
1.Session The core of the mechanism API:javax.servlet.http.HttpSession Interface
2. When the client accesses the server for the first time , And the server executes request.getSession() When the method is used , The server will create a HttpSession Object of type , Then assign a unique identifier to the object sessionid. With cookie In the form of sessionid Back to the client browser . Save to browser memory .
setAttribute(key,value); Sign in ==session.setAttribute("user",user)
Memory shopping cart
3. When you access the server again ,sessionid With cookie Is returned to the server . Server according to sessionid, Find... In memory session object , Get content .
2.session Session failure of ?
1.session There is a default effective time , Default 1800 second .
2.invalidate Method is called .session Lapse immediately ==》 Cancellation / sign out
3. Close the browser . Browser closed ---》 Memory free --》sessionid be without --》 The server cannot be based on sessionid Find the original session Object , Only new .
1. Data needed for a page :request:setAttribute/getAttribute()
2. Data to be displayed on multiple pages :session:setAttribute()/getAttribute()
3. Data shared by all :application==ServletContex:setAttribute()/getAttribute()
Domain Range
3.Cookie and Session The difference between ?
1.Cookie The object of Program ape itself new,Session object Server construction .
2.Cookie The information is stored in the client browser .Session Objects are stored in server memory .
contact :session In the process of mechanism implementation , Using the cookie,sessionid
边栏推荐
- Definition of information entropy
- 小微企业智能名片管理小程序
- Activity registration | play with kubernetes container service improvement class officially opened!
- Multiply Floyd "suggestions collection"
- SQL注入 Less23(过滤注释符)
- shell- 第七章练习
- leetcode 剑指 Offer 27. 二叉树的镜像
- Mlx90640 infrared thermal imager temperature measurement module development notes (V)
- 小区蔬菜配送的小程序
- SQL injection LESS18 (header injection + error injection)
猜你喜欢

【mysql学习08】

用 Redis 做一个可靠的延迟队列
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/c8/fe18308ddad5cb2fbccb79d2d8a2b6.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training

黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。

SQL注入 Less17(报错注入+子查询)

Detailed explanation of zero basis from macro to micro Bert

leetcode 剑指 Offer 27. 二叉树的镜像

一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测

【电子器件笔记5】二极管参数和选型

同事看了我的代码惊呼:居然是这么在Unity中用单例的
随机推荐
LVS负载均衡之LVS-NAT搭建Web群集
The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
Review recitation finishing version
tensorflow 调用多块GPU的一些错误
syncronized锁升级的过程
varest蓝图设置json
Redis之压缩列表ziplist
Some errors of tensorflow calling multiple GPUs
Small and micro enterprise smart business card management applet
SQL language (I)
Getting started with tensorflow
web移动端:touchmove实现局部滚动
Small program of vegetable distribution in community
基于MATLAB的常见线性调制方法
RedisUtil
[dynamic planning] 70. Climbing stairs
SQL注入 Less18(头部注入+报错注入)
倍增Floyd「建议收藏」
Shell - Chapter 8 exercise