文章目錄
-
-
- HttpSession會話管理
- HttpSession的生命周期
- 實踐
-
HttpSession會話管理
HTTP協定是無意識、單向協定。服務端不能主動連接配接用戶端,隻能等待并答複用戶端請求。用戶端連接配接服務端,發出一個HTTP請求,服務端處理請求,并傳回一個HTTP響應給用戶端,至此,本次會話結束。HTTP協定本身不支援服務端儲存用戶端的狀态等資訊。于是,Web伺服器引入了session的概念,用來儲存用戶端的資訊。
方法:HttpSession session = request.getSession(); //擷取Session對象
Session.setAttribute(“username”,”John”); //設定Session中的屬性
原理:
利用伺服器來管理會話的機制,當程式為某個用戶端的請求建立了一個session的時候,伺服器會檢查用戶端的請求是否已經包含了一個session辨別。
URL重寫前面提到過,如果用戶端支援Cookie,那麼生成的URL不變,如果不支援,生成的URL中就會帶有jsessionid字元串的位址。
HttpSession的生命周期
1.建立HttpSession對象
伺服器為每個浏覽器建立不同Session的ID值。使用request.getSession()或request.getSession(true)方法來獲得HttpSession對象。
2.使用HttpSession對象
将産生的sessionID存入到Cookie中;
當用戶端再次發送請求時,會将sessionID與request一起傳送給服務端;
伺服器根據請求過來的sessionID與儲存在伺服器端的session對應起來判斷是否是同一session。
3.HttpSession對象的消亡
将浏覽器關閉;
調用HttpSession的invalidate()方法;
session逾時。
有效期設定
調用Session的setMaxInactiveInterval(long interval)設定;
在web.xml中修改,例如:
< s e s s i o n − c o n f i g > <session-config> <session−config>
< ! − − 會 話 超 時 時 長 為 30 分 鐘 − − > <!-- 會話逾時時長為30分鐘 --> <!−−會話逾時時長為30分鐘−−>
< s e s s i o n − t i m e o u t > 30 < / s e s s i o n − t i m e o u t > <session-timeout>30</session-timeout> <session−timeout>30</session−timeout>
< / s e s s i o n − c o n f i g > </session-config> </session−config>
實踐
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<title>使用者登入</title>
</head>
<body>
<p>使用者登入</p>
<form action="<%=path%>/CheckUser" method="post">
<table border="1" width="250px;">
<tr>
<td width="75px;">使用者名:</td>
<td ><input name="userId"/></td>
</tr>
<tr>
<td width="75px;">密 碼:</td>
<td ><input name="passwd" type="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="送出"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
package com.eshore;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.annotation.WebServlet;
//給目前檔案的路徑設定為根目錄下的GetReaderBody2
@WebServlet(
urlPatterns = { "/CheckUser" },
name = "checkUser"
)
public class CheckUser extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("userId");
String passwd = request.getParameter("passwd");
//判斷是否是linl使用者且密碼相符
if(userId!=null&&"linl".equals(userId)
&&passwd!=null&&"123456".equals(passwd)){
//獲得sessioin對象
HttpSession session = request.getSession();
//設定user參數
session.setAttribute("user", userId);
//跳轉頁面
RequestDispatcher dispatcher = request.
getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request, response);
}else{
RequestDispatcher dispatcher = request.
getRequestDispatcher("/login.jsp");
dispatcher.forward(request, response);
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>歡迎頁面</title>
</head>
<%
String user = (String)session.getAttribute("user");
if(user==null){
%>
<jsp:forward page="login.jsp"/>
<%} %>
<body>
<a href="<%=response.encodeURL(" target="_blank" rel="external nofollow" login.jsp?username=john") %>"></a>
歡迎您:<%=user%>。
</body>
</html>
運作結果: