javaweb學習15:Session(重點)
javaweb學習15:Session(重點)
-
Session:
-
伺服器會給每一個使用者(瀏覽器)建立一個Session物件;
-
一個Session獨佔一個瀏覽器,只要瀏覽器沒有關閉,這個Session就存在;
-
-
Session和Cookie的區別:
-
Cookie:把使用者的資料寫給使用者的瀏覽器;瀏覽器儲存;(可以儲存多個)
-
Session:把使用者的資料寫到使用者獨佔的Session中,伺服器端儲存;(儲存重要資訊,減少伺服器資源的浪費)
-
Session物件由伺服器建立;
-
-
使用場景:
-
儲存登入使用者的資訊;
-
儲存購物車的資訊;
-
在整個網站中經常會使用的資料,我們將它儲存在Session中;
-
-
程式碼案例:建立Session資訊
/**
* Session
*/
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解決亂碼問題
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//得到session
HttpSession session = req.getSession();
session.setAttribute("name",new Person("張三",1));
//獲取session的ID
String id=session.getId();
//判斷session是不是新建立的
if(session.isNew()){
resp.getWriter().write("session建立成功,id為:"+id);
}else{
resp.getWriter().write("session已經在伺服器中存在了");
}
//Session建立的時候做了什麼事情:
/*Cookie cookie = new Cookie("JSESSIOONID",id);
resp.addCookie(cookie);*/
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
-
程式碼案例:獲取Session資訊;
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解決亂碼問題
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//得到session
HttpSession session = req.getSession();
Person person= (Person) session.getAttribute("name");
resp.getWriter().write(person.toString());
System.out.println(person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
-
程式碼案例:登出Session
public class SessionServlet03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//得到session
HttpSession session = req.getSession();
session.removeAttribute("name");//取消session
session.invalidate();//手動登出session
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
-
程式碼案例:會話自動過期:以分鐘為單位
<?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_4_0.xsd"
version="4.0"
metadata-complete="true">
<!--設定Session預設登出時間-->
<session-config>
<!--1分鐘後,session自動失效;以分鐘為單位-->
<session-timeout>1</session-timeout>
</session-config>
</web-app>
相關推薦
javaweb學習15:Session(重點)
javaweb學習15:Session(重點) Session: 伺服器會給每一個使用者(瀏覽器)建立一個Session物件;
JavaSE學習筆記15:集合(一)
集合(一) 1.集合概述 陣列其實就是一個集合,集合實際上就是一個容器,一個載體,可以容納其他型別的資料,一次容納多個物件
我是如何學習寫一個作業系統(三):作業系統的啟動之保護模式
前言 上一篇其實已經說完了boot的大致工作,但是Linux在最後進入作業系統之前還有一些操作,比如進入保護模式。在我自己的FragileOS裡進入保護模式是在載入程式結束後完成的。
我是如何學習寫一個作業系統(二):作業系統的啟動之Bootloader
前言 今天本來的任務看書和把之前寫的FragileOS整理一下,但是到現在還在摸魚,書也只看一點。後來整理了一下寫這個系列的思路,原本的目的是對作業系統原理性的學習和對之前寫的一個玩具型作業系統的回顧,就是想對
我是如何學習寫一個作業系統(四):作業系統之系統呼叫
前言 最近有點事情,馬上要開學了,所以學習的腳步就慢下來了。這一篇主要是來說作業系統的系統呼叫的,像C語言的printf深入到內部就是一個有關螢幕輸出的系統呼叫
我是如何學習寫一個作業系統(八):記憶體管理和段頁機制
前言 多程式和記憶體管理是緊密相連的兩個模組,因為執行程式也就是從記憶體中取指執行,建立程式首先要將程式和資料裝入記憶體。將使用者原程式變成可在記憶體中執行的程式,而這就涉及到了記憶體管理。
深度學習論文翻譯解析(八):Rich feature hierarchies for accurate object detection and semantic segmentation
論文標題:Rich feature hierarchies for accurate object detection and semantic segmentation 標題翻譯:豐富的特徵層次結構,可實現準確的目標檢測和語義分割
深度學習論文翻譯解析(九):Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
論文標題:Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 標題翻譯:用於視覺識別的深度卷積神經網路中的空間金字塔池
深度學習論文翻譯解析(十):Visualizing and Understanding Convolutional Networks
論文標題:Visualizing and Understanding Convolutional Networks 標題翻譯:視覺化和理解卷積網路
ROS中階筆記(四):機器人模擬—Gazebo物理模擬環境搭建(重點)
ROS中階筆記(四):機器人模擬—Gazebo物理模擬環境搭建(重點) 目錄1 ros_control1.1 ros_control安裝2 Gazebo模擬步驟(重點)2.1 配置物理模擬模型2.1.1 第一步:為link新增慣性引數和碰撞屬性2.1.2 第二步:為
C++ STL學習筆記之:佇列(queue)、雙端佇列(deque)與棧(stack)
queue(佇列) 定義一個名為 que 的儲存 int 型別元素的佇列: queue<int> que; 入隊操作:
機器學習中的數學(六):線性判別分析(LDA), 主成分分析(PCA)
前言 如果學習分類演算法,最好從線性的入手,線性分類器最簡單的就是LDA,它可以看做是簡化版的SVM,如果想理解SVM這種分類器,那理解LDA就是很有必要的了。談到LDA,就不得不談談PCA,PCA是一個和LDA非
PHP學習之五:陣列(二)定位陣列元素
定位陣列元素 1、搜尋陣列 in_array() 函式在陣列中搜索一個特定值,如果找到這個值則返回true,否則返回false
Re:通過arch Linux的安裝學習Linux基本指令(Virtualbox)(2)
4.5.掛載分割槽(草率了……上次沒講完) 掛載分割槽的時候注意按順序掛載,先掛在根分割槽到/mnt
Re:通過arch Linux的安裝學習Linux基本指令(Virtualbox)(4)
13.安裝X視窗系統 # pacman -S xorg xorg就是X視窗系統,xorg是X11的一個實現,而X Window System是一個C/S結構的程式,Xorg只是提供了一個X Server,負責底層的操作當你執行一個程式的時候,這個程式會連線到X ser
前端學習:HTML(一)
什麼是 HTML? HTML 是用來描述網頁的一種語言。 HTML 指的是超文字標記語言(英語:Hyper Text Markup Language,簡稱:HTML)
qml 定義函式_Qt 學習之路 2(93):使用 C++ 擴充套件 QML
技術標籤:qml 定義函式 QML 只能執行在一個受限環境中,這是由於 QML 語言本身有一些限制。為了解決這一問題,我們可以使用 C++ 編寫一些功能,供 QML 執行時呼叫。
機器學習sklearn(三十一):Pipeline(管道)和 FeatureUnion(特徵聯合): 合併的評估器
變換器(Transformers)通常與分類器,迴歸器或其他的學習器組合在一起以構建複合估計器。 完成這件事的最常用工具是Pipeline。 Pipeline 經常與 FeatureUnion 結合起來使用。 FeatureUnion 用於將變換器(transformers
(更新:已開放)蘋果關閉 iPhone 13/Pro 系列的 iOS 15 正式版(19A346)韌體驗證
更新:今日(9 月 30 日)午間已重新開放驗證。 9 月 30 日訊息據多位網友投稿,IPSW 查詢網站顯示,蘋果 iPhone 13/Pro 系列四款機型的 iOS15 正式版(19A346)韌體都停止了驗證,目前蘋果官方尚未給出迴應。瞭解到
演算法基礎學習【1】(下):列舉(POJ1222熄燈問題)
原題連結:http://poj.org/problem?id=1222 /* * POJ1222: 題解: 思路: 每個按鈕只需按下一次