当前位置:网站首页>Cookies and sessions
Cookies and sessions
2022-07-29 02:14:00 【TPH-BETTER】
Cookie and Session
- One 、 Conversational Technology
- Two 、Cookie
- 3、 ... and 、JSP Introduction learning
- Four 、Session
- 1. Concept
- 2. Quick start
- 3. Session Principle
- 4. Session The details of the
- 4.1 When the client is shut down , The server does not shut down , Get twice Session Is it the same
- 4.2 The client does not shut down , After the server is shut down , Get twice session Is it the same ?
- 4.3 session When it was destroyed
- 4.4 session Characteristics
- 4.5 Session and cookie The difference between
video https://www.bilibili.com/video/BV1UV411r7fW?p=1&vd_source=3558fd85254f40ca0361146cc92d2cce
One 、 Conversational Technology
1、 conversation : A session contains multiple requests and responses
One session : The first time a browser sends a request to a server resource , Session creation , Until one side is disconnected
2、 function : Between multiple requests within the scope of a session , Shared data
3、 The way :
- Client session technology :Cookie
- Server side session technology :Session
Two 、Cookie
1. Concept
Concept : Client session technology , Save data to client
2. Quick start
Quick start ( Use steps ):
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()
3. Cookie The details of the
3.1 Can I send more than one at a time Cookie?
Sure , You can create multiple Cookie object , Use response Call several times addCookie Method to send Cookie that will do .
3.2 Cookie How long to save in the browser
1、 By default , When the browser is closed ,Cookie The data is destroyed
2、 Persistent storage
setMaxAge(int seconds)
- Positive numbers : take Cookie The data is written to a file on the hard disk , Persistent storage . And designate Cookie Survival time , After the time ,Cookie The file is automatically invalidated
- negative : The default value is ( When the browser is closed ,Cookie The data is destroyed )
- zero : Delete Cookie Information
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 after ,Cookie Support Chinese data
4、cookie Sharing issues ?
1、 Suppose it's in a tomcat Server , Deployed multiple Web project , So in these Web In the project cookie Can we share ?
- By default ,cookie Cannot share
- setPath(String path): Set up cookie The scope of acquisition , By default , Set the current virtual directory , If you need to share , Then you can put path Set to “/”
5、 Different tomcat Server room cookie Sharing issues ?
- setDomain(String path): If the primary domain name is the same , So many servers cookie Can be Shared
example :setDomain(“.baidu.com”), that tieba.baidu.com and news.baidu.com in cookie Can be Shared
3、 ... and 、JSP Introduction learning
1. Concept
JSP:Java Server Pages:java Server side page
- It can be understood as : A special page , You can specify html label , You can also define java Code
- Used to simplify writing
JSP It's essentially one Servlet
2. Use
JSP Script for :JSP Definition Java The way the code works
- <% Code %>: Defined java Code , stay service In the method ,service What can be defined in the method , The script can define what .
- <%! Code %>: Defined javadiam, stay jsp Converted java Class member location
- <%= Code %>: Defined java Code , Will be output to the page . What can be defined in the output statement , The script can define what
JSP Built in objects for
stay jsp There is no need to get and create , Objects that can be used directly
stay jsp There's a total of 9 Built in objects
Let's start with three :
- request
- response
- out: Character output stream object , You can output data to the page , and response.getWriter().write() similar ( stay tomcat Before the server actually responds to the client , Will look for response Buffer data , That is to say, execute first response.getWriter().write(), therefore response.getWriter().write() It will disrupt the order of code execution , Recommended out.write())
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>#[[$Title$]]#</title>
</head>
<body>
<%
System.out.println("hello jsp");
int i=5;
String contextPath=request.getContextPath();
out.print(contextPath);
%>
<%!
int i=3;
%>
<%= "hello" %>
<h1>hi~ jsp!</h1>
<% response.getWriter().write("response");%>
</body>
</html>
Four 、Session
1. Concept
Concept : Server side session technology , Save the data to the server , Sharing data among multiple requests in a session , Save the data in the object on the server side ,HttpSession
2. Quick start
Quick start ( Use steps ):
- obtain HttpSession object :
HttpSession session=request.getSession(); - Use HttpSession object :
Object getAttribute(String name);
void setAttribute(String name,Object value);
void removeAttribute(String name);
Set up Session obtain Session
3. Session Principle
1、Session rely on Cookie, When you first get session Object time , because Cookie Not carrying SessionID, Therefore, it cannot be found in the server memory session object , So it needs to be built session object .
2、 After new construction , hold sessionID Deposit in Cookie in , Respond to the browser , The next time the browser requests the server, it will use Cookie carry SessionID
3、 Server received SessionID after , When calling request.getSession() when , Will go to the memory to find the session object , Therefore, the object obtained at this time is the same as that in the first step session Object is the same
4. Session The details of the
4.1 When the client is shut down , The server does not shut down , Get twice Session Is it the same
By default , Not the same . because SessionID It's dependence Cookie Carry the , By default , When the browser closes ,Cookie It will clear away
If you need the same , You can manually create Cookie, The key is JSESSIONID, Set the maximum lifetime , Give Way Cookie Persistent save . The default is , automatically Cookie That is the first case
Cookie c=new Cookie("JSESSIONID",session.getId());
c.setMaxAge(60*60);
response.addCookie(c);
4.2 The client does not shut down , After the server is shut down , Get twice session Is it the same ?
Not the same . because session Objects are stored in server memory , When the server is down , Memory will be released naturally .
Solutions :
- 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
It should be noted that : stay IDEA Can only achieve passivation , Activation cannot be achieved , Because it will be deleted in the middle work Folder , Rebuild again , The passivated persistent file is missing .tomcat The server can
4.3 session When it was destroyed
- Server down
- session Object call invalidate()
- session Default expiration time 30min
Select configuration modification
<session-config>
<session-timeout>30</session-timeout>
</session-config>
4.4 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
4.5 Session and cookie The difference between
- Session Store data on the server side ,Cookie On the client side
- Session There is no data size limit ,Cookie Yes
- Session Data security ,Cookie Relatively unsafe
边栏推荐
- "Activity recommendation" rush rush! 2022 international open source Festival has new content
- Click the button to slide to the specified position
- “蔚来杯“2022牛客暑期多校训练营2,签到题GJK
- Web crawler API Quick Start Guide
- Form verification hidden input box is displayed before verification
- (arxiv-2018) reexamine the time modeling of person Reid based on video
- 【MySQL】sql给表起别名
- 第十四天:续第十三天标签相关知识
- druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
- leetcode/乘积小于K 的连续子数组的个数
猜你喜欢
试着换个角度理解低代码平台设计的本质
12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance
druid. io kill -9 index_ Realtime traceability task
Cookie和Session
Comprehensive use method of C treeview control
Force deduction brush question (2): sum of three numbers
Rgbd point cloud down sampling
FPGA实现10M多功能信号发生器
Introduction to shared data center agent
Idea connection database
随机推荐
[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)
2022.7.27-----leetcode.592
Opencv image sharpness evaluation (camera autofocus)
点击按钮,下滑到指定的位置
Jetpack--了解ViewModel和LiveData的使用
Lxml web page capture the most complete strategy
LeetCode 练习——剑指 Offer 45. 把数组排成最小的数
[cloud native] what is the microservice architecture
指针——黄金阶段
IDEA 连接 数据库
What is browser fingerprint recognition
Related function records about string processing (long-term update)
[circuit design] convert AC AC to DC
基于C51控制蜂鸣器
移动通信——基于卷积码的差错控制系统仿真模型
Ciscn 2022 central China Misc
忽略微信设置字体
数学建模——带相变材料的低温防护服御寒仿真模拟
字符流综合练习解题过程
表单校验 隐藏的输入框 显示才校验