session對象
session對象在第一個JSP頁面被裝載時自動創建,完成會話期管理。
從一個客戶打開瀏覽器並連接到服務器開始,到客戶關閉瀏覽器離開這個服務器結束,被稱爲一個會話。
當客戶訪問一個服務器時,可能會在服務器的幾個頁面之間切換,服務器應當通過某種辦法知道這是一個客戶,就需要session對象。
session對象是HttpSession類的實例。
常用方法:
public void setAttribute(Sting name, Object value),設定指定名字的屬性的值,並將它添加到session會話範圍內,如果這個屬性是會話範圍內存在,則更改該屬性的值。
public Object getAttribute(String name),在會話範圍內獲取指定名字的屬性的值,返回值類型爲object,如果該屬性不存在,則返回null。
public void removeAttribute(String name),刪除指定名字的session屬性,若該屬性不存在,則出現異常。
public void invalidate(),使session失效。可以立即使當前會話失效,原來會話中存儲的所有對象都不能再被訪問。
public String getId( ),獲取當前的會話ID。每個會話在服務器端都存在一個唯一的標示sessionID,session對象發送到瀏覽器的唯一數據就是sessionID,它一般存儲在cookie中。
public void setMaxInactiveInterval(int interval),設置會話的最大持續時間,單位是秒,負數表明會話永不失效。
public int getMaxInActiveInterval(),獲取會話的最大持續時間。
使用session對象的getCreationTime()和getLastAccessedTime()方法可以獲取會話創建的時間和最後訪問的時間,但其返回值是毫秒,一般需要使用下面的轉換來獲取具體日期和時間。
Date creationTime = new Date(session.getCreationTime());
Date accessedTime = new Date(session.getLastAccessedTime());
實例:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.text.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'session_page_1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>session內置對象</h1>
<hr>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日 HH:MM:ss");
Date d = new Date(session.getCreationTime());
session.setAttribute("password","12345");
session.setAttribute("username","admin");%>
Session創建時間:<%=sdf.format(d) %><br>
Session的ID:<%=session.getId() %><br>
從Session中獲取用戶名:<%=session.getAttribute("username") %><br>
<%
// 設置當前session最大生成期限,單位秒
//session.setMaxInactiveInterval(10);%>
<%
//銷燬session
//session.invalidate(); %>
<a href="session_page_2.jsp" target="_blank">跳轉到Session_page_2.jsp頁面</a>
</body>
</html>
session_page_2.jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'session_page_2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Session的ID:<%=session.getId() %><br>
從Session中獲取用戶名:<%=session.getAttribute("username") %><br>
Session中保存的屬性有:<%
for(int i = 0 ; i < session.getValueNames().length;i++){
out.println(session.getValueNames()[i]);
out.println("  ");
}
%>
</body>
</html>
Session的生命週期:
- 創建:
當客戶第一次訪問某個jsp或者servlet時候,服務器會爲當前會話創建一個SessionId,每次客戶端向服務器發送請求時,都會將此SessionId攜帶過去,服務器端會對此SessionId進行校驗。 - 活動:
某次會話當中通過超鏈接打開的新頁面屬於同一會話。
只要當前會話沒有全部關閉,重新打開新的瀏覽器窗口訪問同一項目資源時屬於同一會話。
除非本次會話的所有頁面都關閉後再重新訪問某個jsp或servlet將會創建新的會話。
注意:注意原有會話還存在只是這個舊的SessionId仍然存在於服務器,只不過再也沒有客戶端會攜帶它然後交予服務器校驗。 - 銷燬:
a.調用Session.invalidate();
b.session過期(超時)
tomcat默認超時時間30分鐘
1.session.setMaxInactiveInterval(時間);//單位秒
2.在web.xml配置:
- 創建:
<session-config>
<session-timeout>10
</session-timeout>
</session-config>//單位分鐘
c.服務器重新啓動