首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

JSP Session 物件完整操作指南

     編輯:關於JSP

Session 基礎

HTTP 無狀態,JSP 透過 HttpSession 維持會話。資料存伺服器端,Session ID 存在客戶端 Cookie(或 URL Rewriting 备援)。預設逾時 30 分鐘,僅能存放物件(不可存基本型別如 int,需用 Integer)。

核心 API

方法說明
getId()唯一 Session 識別碼
getCreationTime()建立時間 (ms)
getLastAccessedTime()最後存取時間 (ms)
getMaxInactiveInterval()逾時秒數,負值=永不逾時
getAttribute(name)取得屬性值 (JSP 1.0 用 getValue)
setAttribute(name, value)設定屬性,覆蓋舊值 (JSP 1.0 用 putValue)

實戰範例:跨頁面傳遞資料

test1.jsp 寫入

<% session.setAttribute("str", new String("this is test")); %>

test2.jsp 讀取

<%
    String ls_str = (String) session.getAttribute("str");
    out.println("從 Session 取出的值:" + ls_str);
%>

瀏覽 test1 再瀏覽 test2 即可見輸出,驗證 Session 跨請求共享特性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved