Session

1.HttpSession

1 )HttpSession:在服務器端保持HTTP狀態信息的方案。和其對應的是Cookie

2  )產生HttpSession對象的過程:當程序需要爲某個客戶端的請求創建一個session時,服務器首先檢查這個客戶端的請求是否包含了一個session標識((即sessionId),如果

已經包含了一個sessionId則說明以前已經爲此客戶端創建過session,服務器就按照session id 把這個session檢索出來使用(如果檢索不到,可能會新建一個,這種情況可能

出現在服務端已經刪除了該用戶對應的session對象,但用戶人爲地請求的URL後面附加上一個JSESSION的參數)。如果客戶端請求不包含sessionId,則爲此客戶端創建一個

session並且生成一個與此session相關聯的sessionId,這個session id 將在本次響應中返回給客戶端保存。

3 )使用Cookie 來跟蹤Session:session通過SessionId來區分不同的客戶,session是以cookie或URL重寫爲基礎,默認使用cookie來實現,系統會創建一個名爲JSESSIONID的輸出cookie,這稱之爲session cookie,session cookie 是存儲在瀏覽器內存中的,並不是寫在硬盤上的,通常看不到JSESSIONID。

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=session.getId()%>
</body>
</html>

瀏覽器第一次訪問服務器,服務器會在響應頭添加Set-Cookie:“JSESSIONID=XXXXXXX”信息,要求客戶端設置cookie,如下圖:

接下來,瀏覽器第二次、第三次...訪問服務器,觀察其請求頭的cookie信息,可以看到JSESSIONID信息存儲在cookie裏,發送給服務器;且響應頭裏沒有Set-Cookie信息

要瀏覽器未關閉,在訪問同一個站點的時候,其請求頭Cookie中的JSESSIONID都是同一個值,被服務器認爲是同一個會話



2.HttpSession 的生命週期:

1). 什麼時候創建 HttpSession 對象

①. 對於 JSP: 是否瀏覽器訪問服務端的任何一個 JSP, 服務器都會立即創建一個 HttpSession 對象呢?
不一定。
> 若當前的 JSP 是客戶端訪問的當前 WEB 應用的第一個資源,且 JSP 的 page 指定的 session 屬性值爲 false, 
則服務器就不會爲 JSP 創建一個 HttpSession 對象;

如訪問第一個頁面是下面的jsp 顯示結果爲 null

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=request.getSession(false)%>
</body>
</html>


> 若當前 JSP 不是客戶端訪問的當前 WEB 應用的第一個資源,且其他頁面已經創建一個 HttpSession 對象,
則服務器也不會爲當前 JSP 頁面創建一個 HttpSession 對象,而回會把和當前會話關聯的那個 HttpSession 對象返回給當前的 JSP 頁面.


②. 對於 Serlvet: 若 Serlvet 是客戶端訪問的第一個 WEB 應用的資源,
則只有調用了 request.getSession() 或 request.getSession(true) 纔會創建 HttpSession 對象


2). page 指令的 session=“false“  到底表示什麼意思?

> 當前 JSP 頁面禁用 session 隱含變量!但可以使用其他的顯式的 HttpSession 對象

3). 在 Serlvet 中如何獲取 HttpSession 對象?

> request.getSession(boolean create): 
create 爲 false, 若沒有和當前 JSP 頁面關聯的 HttpSession 對象, 則返回 null; 若有, 則返回 true
create 爲 true, 一定返回一個 HttpSession 對象. 若沒有和當前 JSP 頁面關聯的 HttpSession 對象, 則服務器創建一個新的
HttpSession 對象返回, 若有, 直接返回關聯的. 

> request.getSession(): 等同於 request.getSession(true)


4). 什麼時候銷燬 HttpSession 對象:

①. 直接調用 HttpSession 的 invalidate() 方法: 該方法使 HttpSession 失效

②. 服務器卸載了當前 WEB 應用. 

③. 超出 HttpSession 的過期時間.

> 設置 HttpSession 的過期時間: session.setMaxInactiveInterval(5); 單位爲秒

> 在tomcat  web.xml 文件中設置 HttpSession 的過期時間: 單位爲 分鐘. 

  <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

④. 並不是關閉了瀏覽器就銷燬了 HttpSession. 

例子

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	SessionID:<%=session.getId()%>
	<br>
	<br> IsNew:<%=session.isNew()%>
	<br>
	<br> MaxInactiveInterval:<%=session.getMaxInactiveInterval()%>
	<br>
	<br> CreateTime:<%=session.getCreationTime()%>
	<br>
	<br> LastAccessTime:<%=session.getLastAccessedTime()%>
	<br>
	<br>
	<%
		Object username = session.getAttribute("username");
		if (username == null) {
			username = "";
		}
	%>
	<form action="hello.jsp">
		username:<input type="text" name="username" value="<%=username%>">
		<input type="submit" value="Submit" />
	</form>
</body>
</html>
hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	SessionId:<%=session.getId()%>
	<br>
	<br> IsNew:<%=session.isNew()%>
	<br>
	<br> MaxInactiveInterval:<%=session.getMaxInactiveInterval()%>
	<br>
	<br> CreateTime:<%=session.getCreationTime()%>
	<br>
	<br> LastAccessTime:<%=session.getLastAccessedTime()%>

	Hello:<%=request.getParameter("username")%>
	<%
		session.setAttribute("username", request.getAttribute("username"));
	%>

	<a href="login.jsp">重新登錄</a>
	<a href="logout.jsp">註銷</a>
</body>
</html>
logout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	SessionID:
	<%=session.getId()%>
	<br>
	<br> IsNew:
	<%=session.isNew()%>
	<br>
	<br> MaxInactiveInterval:
	<%=session.getMaxInactiveInterval()%>
	<br>
	<br> CreateTime:
	<%=session.getCreationTime()%>
	<br>
	<br> LastAccessTime:
	<%=session.getLastAccessedTime()%>
	<br>
	<br> Bye:
	<%=session.getAttribute("username")%>
	<br>
	<br>

	<a href="login.jsp">重新登錄</a>

	<%
		session.invalidate();
	%>
</body>
</html>


瀏覽器禁用cookie後:

每訪問一個頁面sessionId都會變化

如果瀏覽器不支持Cookie,必須自己編程,使用URL重寫的方式來實現Session:

URL重寫

我們知道session依賴Cookie,那麼session爲什麼依賴Cookie呢?因爲服務器需要在每次請求中獲取sessionId,然後找到客戶端的session對象。那麼如果客戶端瀏覽器關閉了Cookie呢?那麼session是不是就會不存在了呢?
其實還有一種方法讓服務器收到的每個請求中都帶有sessioinId,那就是URL重寫!在每個頁面中的每個鏈接和表單中都添加名爲jSessionId的參數,值爲當前sessionid。當用戶點擊鏈接或提交表單時也服務器可以通過獲取jSessionId這個參數來得到客戶端的sessionId,找到sessoin對象。

<form action="<%=response.encodeURL("hello.jsp")%>">
		username:<input type="text" name="username" value="<%=username%>">
		<input type="submit" value="Submit" />
	</form>
<a href="<%=response.encodeURL("login.jsp")%>">重新登錄</a>
	<a href="<%=response.encodeURL("logout.jsp") %>">註銷</a>





發佈了0 篇原創文章 · 獲贊 126 · 訪問量 92萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章