当前位置:网站首页>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
边栏推荐
- 【MySQL】sql给表起别名
- MySQL stores JSON format data
- 数学建模——自来水管道铺设问题
- leetcode/乘积小于K 的连续子数组的个数
- Comprehensive use method of C treeview control
- 基于 ICA 与 DL 的语音信号盲分离
- [UE4] replay game playback for ue4.26
- How companies make business decisions -- with the help of data-driven marketing
- Form verification hidden input box is displayed before verification
- 【RT学习笔记1】RT-Thread外设例程——控制Led灯闪烁
猜你喜欢

MySQL安装常见报错处理大全

【RT学习笔记1】RT-Thread外设例程——控制Led灯闪烁

Verilog procedure assignment statements: blocking & non blocking

Mathematical modeling - location of police stations

autoware中ndtmatching功能加载点云图坐标系修正的问题

Understand the clock tree in STM32 in simple terms

druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11

Complete collection of common error handling in MySQL installation

Solution of Lenovo notebook camera unable to open

"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
随机推荐
Type analysis of demultiplexer (demultiplexer)
数学建模——带相变材料的低温防护服御寒仿真模拟
Opencv image sharpness evaluation (camera autofocus)
The number of consecutive subarrays whose leetcode/ product is less than k
为什么 BI 软件都搞不定关联分析
Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh
JetPack--Navigation实现页面跳转
Comprehensive use method of C treeview control
在Qt中如何编写插件,加载插件和卸载插件
Web crawler API Quick Start Guide
Resolve the conflict with vetur when using eslint, resulting in double quotation marks and comma at the end of saving
Mathematical modeling -- heat conduction of subgrade on Permafrost
[云原生]微服务架构是什么
Force deduction brush question (1): sum of two numbers
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
Promise solves asynchrony
JS dom2 and dom3
Blind separation of speech signals based on ICA and DL
Solution of Lenovo notebook camera unable to open
物联网开发--MQTT消息服务器EMQX