【Servlet】Session会话跟踪技术
Session
Session是指使用HttpSession對象實現會話跟蹤的技術,是一種在服務器端保持會話跟蹤的解決方案。
HttpSession對象是javax.servlet.http.HttpSession接口的實例,也稱為會話對象。
HttpSession對象會在用戶第一次訪問服務器時由容器創建(注意只有訪問JSP、Servlet等程序時才會創建,只訪問HTML、IMAGE等靜態資源并不會創建),當用戶調用其失效方法(invalidate()方法)或超過其最大不活動時間時會失效。在此期間,用戶與服務器之間的多次請求都屬于同一個會話。
Session和Cookie的主要區別在于:
- Cookie是把用戶的數據寫給用戶的瀏覽器。
- Session技術把用戶的數據寫到用戶獨占的Session中。
服務器在創建會話對象時,會為其分配一個唯一的會話標識——SessionId,以“JSESSIONID”的屬性名保存在客戶端Cookie中,在用戶隨后的請求中,服務器通過讀取Cookie中的JSESSIONID屬性值來識別不同的用戶,從而實現對每個用戶的會話跟蹤。
Session工作原理
獲取HttpSession對象
HttpServletRequest接口提供了獲取HttpSession對象的方法:
在程序中第一次執行request.getSession()時,服務器才會創建Session。
HttpSession接口提供的方法
HttpSession接口提供了存取會話域屬性和管理會話生命周期的方法:
Session存取數據
- 存儲會話域屬性:session.setAttribute("username"," haha");
- 會話域中獲取屬性值:String uname = (String)session.getAttribute("username");
- 會話域中刪除屬性:session.removeAttribute("username");
- 用戶登錄示例: 當用戶當用戶登錄成功后,將用戶信息存儲到Session中,并重定向到主頁面顯示用戶信息。
Session生命周期
- Session失效時間:Session具有一定聲生命周期,如果Session超過會話的最大不活動時間,會話自動失效,會話的最大不活動時間指會話超過此時間段不進行任何操作。
- 設置Session的失效時間。
- 方法1:在工程的web.xml中配置Session的生命周期,單位為分鐘。
<Session-config><Session-timeout>15</Session-timeout></Session-config> - 方法2:在程序硬編碼設置。
Session.setMaxInactiveInterval(30 * 60); //設置單位為秒,設置為-1永不過期; - 方法3:在Tomcat安裝目錄下conf/web.xml中配置(Web容器級別)。
<Session-config><Session-timeout>15</Session-timeout></Session-config>
- 方法1:在工程的web.xml中配置Session的生命周期,單位為分鐘。
- 手動銷毀Session:可以通過調用invalidate()方法立即清除會話對象及其所有會話域屬性,同時響應客戶端瀏覽器清除Cookie中的JSessionID,在實際應用中,此方法多用來實現系統的“安全退出”功能。
Session的應用
Session的一個可以存儲用戶信息,可以用于登錄和退出等情況:
- 登錄:用戶信息存入Session中
- 退出:session.invalidate()
設置Session失效時間的三種方式
測試
import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException;public class SetSessionServlet extends HttpServlet {@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}@Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {HttpSession session = request.getSession();session.setAttribute("username", "Jim");session.setAttribute("userAge", 18);response.sendRedirect("GetSessionServlet");}} import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter;public class GetSessionServlet extends HttpServlet {@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}@Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {HttpSession session = request.getSession();String username = (String)session.getAttribute("username");Integer userAge = (Integer)session.getAttribute("userAge");response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.println("用戶名:" + username + "<br/>用戶年齡:" + userAge);}}訪問:localhost:8888/web/SetSessionServlet
重新加載:
把瀏覽器關掉再開(因為我的Firefox暫時不能關,所以用的是Chrome代替):
總結
以上是生活随笔為你收集整理的【Servlet】Session会话跟踪技术的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【操作系统】进程与线程
- 下一篇: 【数值分析】顺序高斯消去法和列主元高斯消