使用Cookie实现记住密码功能
cookie是在客户端的对象存储,
项目:实现网页上的记住密码功能
1.项目结构
2.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.net.*"%>//注意此处引入的包用来使用下面的编码解码
//URLDecoder.decode( ,"utf-8")
//URFEncoder.encode( ,"utf-8")
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="head">
<h1>欢迎访问</h1>
</div>
<%
//防止中文乱码
request.setCharacterEncoding("utf-8");
//判断用户是否选择记住登录
String name = "";
String password="";
Cookie[] cookies = request.getCookies();
if(cookies!=null && cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("name")){
//获取cookie内容并解码
name =URLDecoder.decode( c.getValue(),"utf-8");
} if(c.getName().equals("password")){
password =URLDecoder.decode( c.getValue(),"utf-8"); } }
} //否则为空
%> <div class="main"><form action="dologin.jsp" method="post" class="form"><label>用户名</label>
<!--让下一次访问login.jsp页面时把cookie里面的存的内容填进去-->
<input type="text" name="name" value="<%=name %>"><label>密码</label>
<!--让下一次访问login.jsp页面时把cookie里面的存的内容填进去-->
<input type="text" name="password" value="<%=password %>">
<input type="checkbox" name="isCookie">记住密码
<input type="submit" name="submit" value="登录" class="login">
</form>
</div>
</body>
</html>
3.dologin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.net.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>dologin</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
//判断用户是否选择记住登录,新建cookie对象,是个字符串数组
String[] isCookie= request.getParameterValues("isCookie");
if(isCookie!=null && isCookie.length>0){
//字符编码utf-8格式.防止中文乱码,新的nameCookie,passwordCookie,添加到response
String name= URLEncoder.encode(request.getParameter("name"),"utf-8");
String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
Cookie nameCookie = new Cookie("name",name);
Cookie passwordCookie = new Cookie("password",password);
//设置cookie的有效期10天单位s
nameCookie.setMaxAge(86400);
passwordCookie.setMaxAge(86400);
response.addCookie(nameCookie);
response.addCookie(passwordCookie);
//跳转到welcome.jsp页面
response.sendRedirect("welcome.jsp");
}
%>
</body>
</html>
4.welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.net.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录成功页面</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body>
<% request.setCharacterEncoding("utf-8"); String name = ""; String password="";
//获取已有的cookie Cookie[] cookies = request.getCookies();
//非空遍历cookie if(cookies!=null && cookies.length>0){ for(Cookie c:cookies){
<div class="head"><h1>欢迎Welcome</h1></div><div class="main"><h1>恭喜你<%=loginuser %>登录成功</h1> //这个可以一起不要//遍历到对应的cookie的Name时,取出并解码为utf-8格式,防止乱码 if(c.getName().equals("name")){ name =URLDecoder.decode( c.getValue(),"utf-8"); } if(c.getName().equals("password")){ password =URLDecoder.decode( c.getValue(),"utf-8"); } } } String loginuser=request.getParameter("name");//这个可以不要 %>
姓名:<%=name %><br>密码:<%=password %><br>恭喜你<font color="red"><%=name %></font>登录成功 <br></div> </body></html>
5.知识点
cookie存储需要存入的信息,在下次输入时读取cookie并显示。
1.下次访问时把cookie的内容自动填入:
-->login.jsp
<input type="text" name="name"value="<%=name %>">
2.cookie保存中文:
每个jsp脚本里面先写上:request.setCharaterEncoding("utf-8");
首先要在保存的jsp开头里面引入 import="java.net.*",
再用:String name=URLEncoder.encode(request.getParameter("name"),"utf-8");
两个参数分别表示需要编码的内容,编码的格式.
其次在使用的页面:引入import="java.net.*";
再用:name=URLDecoder.decode(cookie.getValue(),"utf-8")解码
for(Cookie c:cookies){ if(c.getName().equals("name")){ name =URLDecoder.decode( c.getValue(),"utf-8"); } if(c.getName().equals("password")){ password =URLDecoder.decode( c.getValue(),"utf-8"); } }
3.创建Cookie并添加到cookie(dologin.jsp):
String name= URLEncoder.encode(request.getParameter("name"),"utf-8"); String password = URLEncoder.encode(request.getParameter("password"),"utf-8"); Cookie nameCookie = new Cookie("name",name); Cookie passwordCookie = new Cookie("password",password); //设置cookie的有效期10天单位s nameCookie.setMaxAge(86400); passwordCookie.setMaxAge(86400); response.addCookie(nameCookie); response.addCookie(passwordCookie);
4.获取Cookie:(login.jsp,welcome.jsp)
Cookie[] cookies = request.getCookies();
if(cookies!=null && cookies.length>0){
for(Cookie c:cookies){
//获取cookie内容并解码
if(c.getName().equals("name")){
name =URLDecoder.decode( c.getValue(),"utf-8");
}
5.设置cookie生命周期:
//设置cookie的有效期10天单位s nameCookie.setMaxAge(86400);