1. 程式人生 > >JavaWeb-使用cookie完成兩週內免登入功能

JavaWeb-使用cookie完成兩週內免登入功能

歡迎頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>歡迎頁面</title>

</head>

<body>
	<h2>歡迎頁面</h2>
	<br>
	<%
		//判斷是否存在使用者名稱cookie,存在就輸出,不存在跳轉到登入頁面
		Cookie cookie = null;
		Cookie[] cookies = null;
		cookies = request.getCookies();
		cookie = getCookieByName(cookies, "username");
		if (cookie != null) {
			out.print("登陸成功!   welcome!" + cookie.getValue()
					+ "!---cookie方式");
		} else if (session.getAttribute("username") != null) {
			out.print("登陸成功!   Welcome!" + session.getAttribute("username")
					+ "!---session方式");
		} else {
			response.sendRedirect("login.jsp");
		}
	%>
	<%!// 建立方法,用於查詢指定名稱的cookie
	public static Cookie getCookieByName(Cookie[] cs, String name) {
		if (cs == null || cs.length == 0) {
			return null;
		}
		for (Cookie c : cs) {
			if (name.equals(c.getName())) {
				return c;
			}
		}
		return null;
	}%>
</body>
</html>

邏輯判斷頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>邏輯判斷</title>



</head>

<body>
	<%
		//防止錯誤資訊重複出現
		session.removeAttribute("kong");
		session.removeAttribute("ywlj");
		session.removeAttribute("cuowu");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (username.equals("") || password.equals("")) {
			//錯誤跳轉,使用者名稱密碼有一項為空就跳轉到登入頁面,並返回錯誤資訊
			session.setAttribute("kong", "<h3>您輸入的使用者名稱或密碼為空,請重新輸入</h3>");
			response.sendRedirect("login.jsp");
		} else {
			if (username.matches("[a-zA-Z]{3,12}")
					&& password.matches("[a-zA-Z0-9]{6,12}")) {
				if (username.equals("tom") && password.equals("123456")) {
					//判斷是否兩週免登陸,是否勾選複選框
					if (request.getParameter("keep") != null) {
						Cookie usernameCookie = new Cookie("username",
								username);
						usernameCookie.setMaxAge(60 * 60 * 24 * 7 * 2);
						response.addCookie(usernameCookie);
					} else {
						session.setAttribute("username", username);
					}
					//登陸成功,跳轉到歡迎頁面
					response.sendRedirect("index.jsp");

				} else {
					//錯誤跳轉,使用者名稱密碼有一項不正確就跳轉到登入頁面,並返回錯誤資訊
					session.setAttribute("cuowu",
							"<h3>您輸入的使用者名稱或密碼錯誤,請重新輸入</h3>");
					response.sendRedirect("login.jsp");
				}
			} else {
				//錯誤跳轉,使用者名稱密碼有一項不符合業務邏輯就跳轉到登入頁面,並返回錯誤資訊
				session.setAttribute("ywlj",
						"<h3>您輸入的使用者名稱或密碼不符合規則,請重新輸入</h3>");
				response.sendRedirect("login.jsp");

			}

		}
	%>

	<br>
</body>
</html>

登入頁面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>登入頁面</title>



</head>

<body>
	<h2>登入</h2>
	<br>
	<form action="login-action.jsp" method="post">
		使用者名稱<input name="username">(只能由字母組成,3~12位)<br> 密碼<input
			type="password" name="password">(6~12位)<br> <input
			type="checkbox" name="keep" > 兩週免登陸<br> <input
			type="submit" value="登入">


	</form>
	<hr>
	${sessionScope["kong"] } ${sessionScope["ywlj"] }
	${sessionScope["cuowu"] }
</body>
</html>

效果

未登入前,只有一個cookie

登入且勾選兩週免登陸

下次可以輸入網址直接進入歡迎頁面