利用cookie实现记住密码功能

登陆界面:login.jsp

form表单

<form action="dologin.jsp" method="post" >

用户名:<input type="text" name="uname"/>

<br/>

密码:<input type="password" name="upwd" />

<br/>

记住密码:

<input type="checkbox" name="checkpwd" value="1"/>记住密码

<br/>

<input type="submit" value="登录"/>

</form>

在 dologin.jsp 中接收并处理提交的数据:

java 复制代码
<%@page import="com.hz.dao.impl.UserDaoImpl"%>
<%@page import="com.hz.dao.UserDao"%>
<%@page import="com.hz.pojo.User"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dologin.jsp页面处理</title>
</head>
<body>
	<%
		//post之中文乱码
		request.setCharacterEncoding("utf-8");

		String uname = request.getParameter("uname");
		String upwd = request.getParameter("upwd");

		//是否记住密码
		String checkpwd = request.getParameter("checkpwd");

		//调用dao方法
		UserDao userDao = new UserDaoImpl();
		User user = userDao.loginUser(uname, upwd);
		//5s清除会话
		//session.setMaxInactiveInterval(5);
		//逻辑判断是否成功	
		if (user != null) {

			//添加自定义属性
			session.setAttribute("user", user);
			//设置session非活动时间          单位秒        60
			session.setMaxInactiveInterval(10); 
			
			if("1".equals(checkpwd)){
				//存cookie
				Cookie cookieuname = new Cookie("uname",user.getUserCode());
	
				cookieuname.setPath("/");//设置路径
				cookieuname.setMaxAge(60*60);
				//将cookie放入response对象
			
				//存cookie
				Cookie cookieupwd = new Cookie("upwd",user.getUserPassword());
	
				cookieuname.setPath("/");//设置路径
				cookieuname.setMaxAge(60*60);
				//将cookie放入response对象
				response.addCookie(cookieuname);
				response.addCookie(cookieupwd);
				
			}
			
			
			response.sendRedirect(request.getContextPath() +"/index.jsp");
			
			

		} else {
			//失败,跳转登录页面
			//重定向
			response.sendRedirect(request.getContextPath() + "/login.jsp");
			//request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	%>
</body>
</html>

---------------------------------------------------------------------------------

在login.jsp页面实现接收已存的值

java 复制代码
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
	 <%
	 	String uname="";
	 	String upwd="";
	 	Cookie cookies[] = request.getCookies();
	 	if(cookies != null && cookies.length>0){
	 		
	 		for(Cookie ck:cookies){
	 			
	 			if(ck.getName().equals("uname")){
	 				uname = ck.getValue();
	 			}
	 			if(ck.getName().equals("upwd")){
	 				upwd = ck.getValue();
	 			}
	 			
	 		}
	 	
	 	}

	 %>
	 
	<form action="dologin.jsp" method="post" >
		用户名:<input type="text" name="uname" value="<%=uname %>" />
		<br/>
		密码:<input type="password" name="upwd" value=<%=upwd %>>
		<br/>
		记住密码:
		<input type="checkbox" name="checkpwd" value="1"/>记住密码
		<br/>
		<input type="submit" value="登录"/>
	</form>

</body>
</html>
相关推荐
Struggler2817 分钟前
Chrome插件开发
前端
大葱白菜9 分钟前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·后端
大葱白菜10 分钟前
Java Map 集合详解:从基础语法到实战应用,彻底掌握键值对数据结构
java·后端
添乱10 分钟前
「Java案例」判断是否是闰年的方法
java
FG.14 分钟前
Day22
java·面试
菜鸟的迷茫16 分钟前
Redis 缓存雪崩、穿透、击穿面试题深度解析与 Spring Boot 实战代码示例
java
前端 贾公子19 分钟前
Monorepo + vite 怎么热更新
前端
珹洺27 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
SHUIPING_YANG35 分钟前
根据用户id自动切换表查询
java·服务器·数据库
爱吃烤鸡翅的酸菜鱼1 小时前
IDEA高效开发:Database Navigator插件安装与核心使用指南
java·开发语言·数据库·编辑器·intellij-idea·database