利用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>
相关推荐
陈_杨2 分钟前
鸿蒙5开发宝藏案例分享---切面编程实战揭秘
前端
Java知识库7 分钟前
「深度拆解」Spring Boot如何用DeepSeek重构MCP通信层?从线程模型到分布式推理的架构进化
java·开发语言·spring boot·程序员·编程
喵手9 分钟前
CSS3 渐变、阴影和遮罩的使用
前端·css·css3
顽强d石头11 分钟前
bug:undefined is not iterable (cannot read property Symbol(Symbol.iterator))
前端·bug
烛阴20 分钟前
模块/命名空间/全局类型如何共存?TS声明空间终极生存指南
前端·javascript·typescript
进阶的DW20 分钟前
新手小白使用VMware创建虚拟机安装Linux
java·linux·运维
火车叼位24 分钟前
Git 精准移植代码:cherry-pick 简单说明
前端·git
oioihoii24 分钟前
C++11 尾随返回类型:从入门到精通
java·开发语言·c++
江城开朗的豌豆27 分钟前
JavaScript篇:移动端点击的300ms魔咒:你以为用户手抖?其实是浏览器在搞事情!
前端·javascript·面试
华洛34 分钟前
聊聊我们公司的AI应用工程师每天都干啥?
前端·javascript·vue.js