当前位置:网站首页>Session learning diary 1
Session learning diary 1
2022-07-04 03:18:00 【herb. dr】
Catalog
1.4 Session And Request Application difference
Two 、 Browser disable Cookie Solution
2.1 Browser disable Cookie The consequences of
One 、Session object
1.1 Session summary
1、Session Used to record the user's status . Session It means in a For a while , Single client and Web A series of related interactive processes of the server .
2、 In a Session in , Customers may request access to the same resource multiple times , It is also possible to request access to various server resources .
1.2 Session principle
1、 The server will assign one for each session Session object
2、 Multiple requests from the same browser , Belong to the same session (Session)
3、 First use to Session when , The server will automatically create Session, And create Cookie Storage Sessionld Of ID Send back to client
Be careful :session It is created by the server .
1.3 Session Use
1、Session Scope : Have space to store data , The scope is valid for one session
(1) A session is multiple requests sent using the same browser . One Once the browser closes , Ends the session
(2) Data can be stored in Session in , Get anywhere in a session
(3) Can transfer any data ( Basic data type 、 object 、 aggregate 、 Array )
2、 obtain Session
HttpSession session = request.getSession();
System.out.println("Id:" + session.getID()); // The only mark
Code :
package com.ha.session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session.getId());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<servlet>
<servlet-name>ss</servlet-name>
<servlet-class>com.ha.session.SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ss</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
</web-app>
If you close the browser , Revisit , To get the sessionID It's different , Prove that after the last session session It was destroyed
3、Session Save the data
settrbute( Property name , 0bject); Save data to session in
session.setAttribute("key", value); // Stored in... As key value pairs session Scope .
4、Session get data
getAttribute( Property name ); obtain session Data in the
session.getAttribute("key"); // adopt String Type of key visit 0bject Type of value
5、Session Remove data
removeAtribute( Property name ); from session Delete data from
session.removeAttribute("key"); // Remove by key session Value in scope
6、 Code
package com.ha.session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. adopt request Object acquisition Session object
HttpSession session = request.getSession();
//2. Use session Save the data
session.setAttribute("username","HuAn");
session.setAttribute("password","123456");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
package com.ha.session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class GetValueServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. adopt request obtain session object
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
System.out.println(" from session Got :"+username);
System.out.println(" from session Got :"+password);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
If you show it again session I executed this paragraph before , Then you can't get username 了 , Start with removal , All access to username All the contents obtained are NULL
1.4 Session And Request Application difference
request It's one Valid requests , Request change , be request change
session It's one Session is valid , Browser changes , be session change
Code :
modify SessionServlet.java
modify GetValueServlet.java
visit ss Jump to gv
1.5 Session Life cycle of
Start : The first time I used Session The request generated , Create Session
end :
Browser closed , Failure
Session Overtime , Failure
session.setMaxlnactiveIntervalseconds); // Set the maximum effective time ( Company : second )
Hand destroy , Failure
session.invalidate(); // Log in to exit 、 Cancellation
Visit the one above again Java file , For the second Java The generated browser closes when it closes session The destruction time of has been modified .
The following code , Every time I get session It's all different .
Two 、 Browser disable Cookie Solution
2.1 Browser disable Cookie The consequences of
The server is by default , Will use Cookie The way to sessionID Send it to the browser , If the user prohibits Cookie be sessionlD Will not be saved by the browser , At this time, the server can use such as URL Rewrite this way to send sessionID.
2.2 URL rewrite
When the browser accesses an address on the server , No longer use the original address , Instead, use a rewritten address ( That is, add... After the original address sessionID).
Splicing , Then jump to the code :
边栏推荐
- Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
- 長文綜述:大腦中的熵、自由能、對稱性和動力學
- The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
- Don't disagree, this is the most powerful "language" of the Internet
- Backpropagation formula derivation [Li Hongyi deep learning version]
- WP collection plug-in free WordPress collection hang up plug-in
- Explain AI accelerator in detail: why is this the golden age of AI accelerator?
- POSTECH | option compatible reward reverse reinforcement learning
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Tsinghua University product: penalty gradient norm improves generalization of deep learning model
猜你喜欢
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
Add IDM to Google browser
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Li Chuang EDA learning notes IX: layers
長文綜述:大腦中的熵、自由能、對稱性和動力學
Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
Want to do something in production? Then try these redis commands
Teach you how to optimize SQL
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
随机推荐
[UE4] parse JSON string
Code Execution Vulnerability - no alphanumeric rce create_ function()
false sharing
Résumé des outils communs et des points techniques de l'examen PMP
Node write API
POSTECH | option compatible reward reverse reinforcement learning
MySQL workbench use
1day vulnerability pushback skills practice (3)
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
[latex] production of complex tables: excel2latex and detail adjustment
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
Handler source code analysis
what does ctrl + d do?
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Setting methods, usage methods and common usage scenarios of environment variables in postman
PTA tiantisai l1-079 tiantisai's kindness (20 points) detailed explanation
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
How to use STR function of C language