JavaWeb-使用cookie完成两周内免登录功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41690324/article/details/83961081

欢迎页面

<%@ 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

登录且勾选两周免登陆

下次可以输入网址直接进入欢迎页面

扫描二维码关注公众号,回复: 4167772 查看本文章
这个任务做了好几天,怎么做都是有点小毛病
今天全部重新写了一遍,终于好了

猜你喜欢

转载自blog.csdn.net/qq_41690324/article/details/83961081