利用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>
相关推荐
十八朵郁金香16 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
菜鸟蹦迪22 分钟前
八股文实战之JUC:ArrayList不安全性
java
2501_9032386522 分钟前
Spring MVC配置与自定义的深度解析
java·spring·mvc·个人开发
逻各斯33 分钟前
redis中的Lua脚本,redis的事务机制
java·redis·lua
计算机毕设指导635 分钟前
基于Springboot学生宿舍水电信息管理系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
计算机-秋大田42 分钟前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
计算机小白一个1 小时前
蓝桥杯 Java B 组之背包问题、最长递增子序列(LIS)
java·数据结构·蓝桥杯
m0_528723811 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer1 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
二十雨辰1 小时前
[Java基础]网络编程
java·开发语言