loginProcess.jspsession.setAttribute("name", username) → 顯示成功並提供跳轉鏈接session.getAttribute("name") 讀取 → 判斷是否登錄 → 顯示歡迎或提示超時重新登錄<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用戶登錄</title>
<style>
body {font-family: sans-serif; background:#f5f5f5; display:flex; justify-content:center; padding-top:100px;}
.login-box {background:#fff; padding:30px; border-radius:8px; box-shadow:0 2px 10px rgba(0,0,0,.1); width:320px;}
h2 {text-align:center; margin-bottom:24px; color:#333;}
.form-group {margin-bottom:16px;}
label {display:block; margin-bottom:6px; font-size:14px; color:#666;}
input[type=text], input[type=password] {width:100%; padding:10px; border:1px solid #ddd; border-radius:4px; box-sizing:border-box;}
button {width:100%; padding:12px; background:#007bff; color:#fff; border:none; border-radius:4px; font-size:16px; cursor:pointer;}
button:hover {background:#0056b3;}
</style>
</head>
<body>
<div class="login-box">
<h2>系統登錄</h2>
<form action="loginProcess.jsp" method="post">
<div class="form-group">
<label>用戶名</label>
<input type="text" name="username" placeholder="請輸入用戶名" required>
</div>
<div class="form-group">
<label>密碼</label>
<input type="password" name="password" placeholder="請輸入密碼" required>
</div>
<button type="submit">登錄</button>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
// 1. 獲取參數(實際項目應做防 XSS、加密校驗)
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2. 簡單演示:固定帳號 admin/123456
boolean valid = "admin".equals(username) && "123456".equals(password);
if (valid) {
// 3. 寫入 Session,默認 30 分鐘無操作過期
session.setAttribute("loginUser", username);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登錄成功</title>
<style>body{font-family:sans-serif;text-align:center;padding-top:100px;} a{color:#007bff;text-decoration:none;} a:hover{text-decoration:underline;}</style>
</head>
<body>
<h2>歡迎,<%= username %>!登錄成功</h2>
<p><a href="welcome.jsp">進入系統首頁</a></p>
</body>
</html>
<%
} else {
%>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>登錄失敗</title></head>
<body style="font-family:sans-serif;text-align:center;padding-top:100px;">
<h2 style="color:#dc3545;">用戶名或密碼錯誤</h2>
<p><a href="login.jsp">返回重新登錄</a></p>
</body>
</html>
<%
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String loginUser = (String) session.getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系統首頁</title>
<style>body{font-family:sans-serif;max-width:600px;margin:80px auto;padding:0 20px;} .card{background:#fff;padding:30px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,.1);} .btn{display:inline-block;margin-top:20px;padding:10px 20px;background:#dc3545;color:#fff;text-decoration:none;border-radius:4px;}</style>
</head>
<body>
<div class="card">
<%
if (loginUser != null) {
%>
<h2>儀表盤</h2>
<p>當前登錄用戶:<%= loginUser %></p>
<p>這是受保護的頁面,只有登錄用戶可訪問。</p>
<%
} else {
%>
<h2 style="color:#dc3545;">會話已過期或未登錄</h2>
<p>請重新登錄。</p>
<%
}
%>
<a href="login.jsp" class="btn">退出登錄 / 重新登錄</a>
</div>
</body>
</html>
| API | 說明 |
|---|---|
session.setAttribute(name, value) | 存放會話屬性,全局唯一 Session 實例(同一瀏覽器會話) |
session.getAttribute(name) | 讀取屬性,返回 Object 需強轉,不存在返回 null |
session.invalidate() | 銷毀 Session(登出時調用) |
session.setMaxInactiveInterval(seconds) | 程序化設置超時秒數(優先於 web.xml) |
request.getSession(false) | 不創建新 Session,僅獲取現有(避免匿名用戶產生垃圾 Session) |
Session Replication、Redis Spring Session、Sticky Session (Nginx ip_hash)。session.getAttribute("loginUser"),未登錄重定向到登錄頁。session.invalidate() 再 request.getSession(true) 重建。JSP 內置 session 對象(HttpSession)提供了簡單易用的會話管理能力。通過 登錄寫入 → 訪問讀取 → 登出銷毀 三步驟,即可實現基礎的用戶認證授權流程。實際項目中請結合安全框架、集群方案進行加固。