SlideShare a Scribd company logo
4
• 會話管理
學習目標
• 了解會話管理基本原理
• 使用 Cookie 類別
• 使用 HttpSession 會話管理
• 了解容器會話管理原理
會話管理基本原理
• 每個請求對伺服器來說都是新的訪客請求
• 每次請求時「主動告知」伺服器多次請求間
必要的資訊
使用隱藏欄位
使用隱藏欄位
使用 Cookie
使用 Cookie
• Cookie 的設定是透過 set-cookie 標頭
• 必須在實際回應瀏覽器之前使用
addCookie() 來新增 Cookie 實例
• 瀏覽器輸出 HTML 回應之後再執行
addCookie() 是沒有作用的
使用 Cookie
• 可以使用 setMaxAge() 設定 Cookie 的有效
期限,設定單位是「秒」
• 預設關閉瀏覽器之後 Cookie 就失效
取得 Cookie
• HttpServletRequest 的 getCookies()
取得 Cookie
• Cookie 若要避免被竊取,可以透過 Cookie
的 setSecure()設定 true,那麼就只會在
連線有加密(HTTPS)的情況下傳送 Cookie。
• 在 Servlet 3.0 ,Cookie 新增了
setHttpOnly()方法
– 會在 set-cookie 標頭上附加 HttpOnly 屬性,
在瀏覽器支援的情況下,這個 Cookie 將不會被
客戶端腳本(例如 JavaScript)讀取
– 使用 isHttpOnly() 得知一個 Cookie 是否被
setHttpOnly()
使用 URI 重寫
使用 HttpSession
• 用 HttpServletRequest 的
getSession() 取得 HttpSession 物件
• 會話範圍屬性
– setAttribute()
– getAttribute()
使用 HttpSession
• 預設關閉瀏覽器前,取得的 HttpSession
都是相同的實例
• 直接讓目前的 HttpSession 失效,可以執
行 HttpSession 的 invalidate()
使用 HttpSession
HttpSession 會話管理原理
HttpSession 會話管理原理
HttpSession 自動失效
• 執行 HttpSession 的
setMaxInactiveInterval() 方法,單
位是「秒」
• 在 web.xml 設定
HttpSession 自動失效
• HttpSession 物件在瀏覽器多久沒活動就
失效的時間
• 不是儲存 Session ID 的 Cookie 失效時間
• 儲存 Session ID 的 Cookie 預設為關閉瀏覽器
就失效
SessionCookieConfig
• Servlet 3.0 新增,可在 web.xml 設定
HttpSession 與 URI 重寫
• HttpSession 預設用 Cookie 存 Session ID
• 在使用者禁用 Cookie 的情況下,仍打算運用
HttpSession 來進行會話管理,那麼可以
搭配 URI 重寫的
• 可以使用 HttpServletResponse 的
encodeURL() 協助產生所需的 URI 重寫
HttpSession 與 URI 重寫
• encodeURL()
• encodeRedirectURL()

More Related Content

What's hot

CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式
Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Justin Lin
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Justin Lin
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
Justin Lin
 
Java SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - LambdaJava SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - Lambda
Justin Lin
 
Spring boot
Spring bootSpring boot
Spring boot
Bhagwat Kumar
 
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Justin Lin
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
Aaron Schram
 
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDEJava SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
Justin Lin
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Pei-Tang Huang
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jaran Flaath
 
Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件
Justin Lin
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
Justin Lin
 
Spring boot
Spring bootSpring boot
Spring boot
Gyanendra Yadav
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
Josué Neis
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti
 

What's hot (20)

CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
 
Java SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - LambdaJava SE 8 技術手冊第 12 章 - Lambda
Java SE 8 技術手冊第 12 章 - Lambda
 
Spring boot
Spring bootSpring boot
Spring boot
 
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
Java SE 8 技術手冊第 14 章 - NIO 與 NIO2
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDEJava SE 8 技術手冊第 2 章 - 從JDK到IDE
Java SE 8 技術手冊第 2 章 - 從JDK到IDE
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件Java SE 8 技術手冊第 4 章 - 認識物件
Java SE 8 技術手冊第 4 章 - 認識物件
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
Spring boot
Spring bootSpring boot
Spring boot
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring framework core
Spring framework coreSpring framework core
Spring framework core
 

Similar to Ch04 會話管理

Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Justin Lin
 
.NET Security Application/Web Development - Part II
.NET Security Application/Web Development - Part II.NET Security Application/Web Development - Part II
.NET Security Application/Web Development - Part II
Chen-Tien Tsai
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Justin Lin
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
twMVC
 
Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊
Chung-Yi Huang
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送yongboy
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
yongboy
 
移动搜索在慢网速下的优化
移动搜索在慢网速下的优化移动搜索在慢网速下的优化
移动搜索在慢网速下的优化
Charlee Green
 
網站系統安全及資料保護設計認知
網站系統安全及資料保護設計認知網站系統安全及資料保護設計認知
網站系統安全及資料保護設計認知
Justin Lin
 
新浪微博Feed服务架构
新浪微博Feed服务架构新浪微博Feed服务架构
新浪微博Feed服务架构
XiaoJun Hong
 
用戶端攻擊與防禦
用戶端攻擊與防禦用戶端攻擊與防禦
用戶端攻擊與防禦
Taien Wang
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4
Daniel Chou
 
Html5
Html5Html5
Html5
cazhfe
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesJeff Chu
 
企業導入微服務實戰 - updated
企業導入微服務實戰 - updated企業導入微服務實戰 - updated
企業導入微服務實戰 - updated
Paul Chao
 
廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰
Paul Chao
 
集群Session共享
集群Session共享集群Session共享
集群Session共享
dhlzj
 
客户端存储与计算
客户端存储与计算客户端存储与计算
客户端存储与计算xiaotao ning
 
開發環境建置
開發環境建置開發環境建置
開發環境建置
Shengyou Fan
 

Similar to Ch04 會話管理 (20)

Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
 
.NET Security Application/Web Development - Part II
.NET Security Application/Web Development - Part II.NET Security Application/Web Development - Part II
.NET Security Application/Web Development - Part II
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
 
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
雲端環境的快取策略-Global Azure Bootcamp 2015 臺北場
 
Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊Google雲端備份與虛擬伺服器建置工作坊
Google雲端備份與虛擬伺服器建置工作坊
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
 
移动搜索在慢网速下的优化
移动搜索在慢网速下的优化移动搜索在慢网速下的优化
移动搜索在慢网速下的优化
 
網站系統安全及資料保護設計認知
網站系統安全及資料保護設計認知網站系統安全及資料保護設計認知
網站系統安全及資料保護設計認知
 
新浪微博Feed服务架构
新浪微博Feed服务架构新浪微博Feed服务架构
新浪微博Feed服务架构
 
用戶端攻擊與防禦
用戶端攻擊與防禦用戶端攻擊與防禦
用戶端攻擊與防禦
 
How to ASP.NET MVC4
How to ASP.NET MVC4How to ASP.NET MVC4
How to ASP.NET MVC4
 
Html5
Html5Html5
Html5
 
Introduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 FeaturesIntroduction to ASP.NET MVC and MVC 5 Features
Introduction to ASP.NET MVC and MVC 5 Features
 
企業導入微服務實戰 - updated
企業導入微服務實戰 - updated企業導入微服務實戰 - updated
企業導入微服務實戰 - updated
 
廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰廣宣學堂: 企業導入微服務實戰
廣宣學堂: 企業導入微服務實戰
 
集群Session共享
集群Session共享集群Session共享
集群Session共享
 
客户端存储与计算
客户端存储与计算客户端存储与计算
客户端存储与计算
 
開發環境建置
開發環境建置開發環境建置
開發環境建置
 

More from Justin Lin

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring Boot
Justin Lin
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMail
Justin Lin
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理
Justin Lin
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤
Justin Lin
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題
Justin Lin
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步
Justin Lin
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能
Justin Lin
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組
Justin Lin
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
Justin Lin
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構
Justin Lin
 
8. open() 與 io 模組
8. open() 與 io 模組8. open() 與 io 模組
8. open() 與 io 模組
Justin Lin
 
7. 例外處理
7. 例外處理7. 例外處理
7. 例外處理
Justin Lin
 
6. 類別的繼承
6. 類別的繼承6. 類別的繼承
6. 類別的繼承
Justin Lin
 
5. 從模組到類別
5. 從模組到類別5. 從模組到類別
5. 從模組到類別
Justin Lin
 
4. 流程語法與函式
4. 流程語法與函式4. 流程語法與函式
4. 流程語法與函式
Justin Lin
 
3.型態與運算子
3.型態與運算子3.型態與運算子
3.型態與運算子
Justin Lin
 
1. Python起步走
1. Python起步走1. Python起步走
1. Python起步走
Justin Lin
 
2. 從 REPL 到 IDE
2. 從 REPL 到 IDE2. 從 REPL 到 IDE
2. 從 REPL 到 IDE
Justin Lin
 
Python 3.9 技術手冊目錄
Python 3.9 技術手冊目錄Python 3.9 技術手冊目錄
Python 3.9 技術手冊目錄
Justin Lin
 
CH19:深入模組化
CH19:深入模組化CH19:深入模組化
CH19:深入模組化
Justin Lin
 

More from Justin Lin (20)

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring Boot
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMail
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構
 
8. open() 與 io 模組
8. open() 與 io 模組8. open() 與 io 模組
8. open() 與 io 模組
 
7. 例外處理
7. 例外處理7. 例外處理
7. 例外處理
 
6. 類別的繼承
6. 類別的繼承6. 類別的繼承
6. 類別的繼承
 
5. 從模組到類別
5. 從模組到類別5. 從模組到類別
5. 從模組到類別
 
4. 流程語法與函式
4. 流程語法與函式4. 流程語法與函式
4. 流程語法與函式
 
3.型態與運算子
3.型態與運算子3.型態與運算子
3.型態與運算子
 
1. Python起步走
1. Python起步走1. Python起步走
1. Python起步走
 
2. 從 REPL 到 IDE
2. 從 REPL 到 IDE2. 從 REPL 到 IDE
2. 從 REPL 到 IDE
 
Python 3.9 技術手冊目錄
Python 3.9 技術手冊目錄Python 3.9 技術手冊目錄
Python 3.9 技術手冊目錄
 
CH19:深入模組化
CH19:深入模組化CH19:深入模組化
CH19:深入模組化
 

Ch04 會話管理