当前位置:网站首页>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 :

边栏推荐
- MySQL workbench use
- The difference between MCU serial communication and parallel communication and the understanding of UART
- [latex] production of complex tables: excel2latex and detail adjustment
- The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
- Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- Webhook triggers Jenkins for sonar detection
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
- Network communication basic kit -- IPv4 socket structure
猜你喜欢

Johnson–Lindenstrauss Lemma

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex

Rhcsa day 2

What are the conditions for the opening of Tiktok live broadcast preview?

Consul of distributed service registration discovery and unified configuration management
![[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love

Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
![Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]](/img/0d/7a8370d153a8479b706377c3487220.jpg)
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]

The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult

false sharing
随机推荐
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
VRRP+BFD
Practical multifunctional toolbox wechat applet source code / support traffic master
What is cloud primordial?
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
1day vulnerability pushback skills practice (3)
MySQL query
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
Backpropagation formula derivation [Li Hongyi deep learning version]
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
WordPress collection WordPress hang up collection plug-in
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
@Scheduled scheduled tasks
The difference between MCU serial communication and parallel communication and the understanding of UART
Short math guide for latex by Michael downs
How to use websocket to realize simple chat function in C #
MySQL is dirty
Problems and solutions of several concurrent scenarios of redis
Explain AI accelerator in detail: why is this the golden age of AI accelerator?