login.jsp?
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
<head>
<%@?page?import="java.net.*"?%>
<%@?page?language="java"?contentType="text/html;?charset=utf-8"
????pageEncoding="utf-8"%>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>???????</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8");
String?username="";
String?password="";
Cookie[]?cookie=request.getCookies();
if(cookie!=null&&cookie.length>0){
for(Cookie?c:cookie){
if(c.getName().equals("username")){
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
???<form?action="dologin.jsp"?method="post">
??????<table>
?????????<tr>
???????????<td>用戶(hù)名:</td>
???????????<td><input?type="text"?name="username"?value="<%=username?%>"/></td>
?????????</tr>
??????????<tr>
???????????<td>密碼:</td>
???????????<td><input?type="password"?name="password"?value="<%=password?%>"/></td>
?????????</tr>
?????????<tr>
???????????<td?colspan="2"><input?type="checkbox"?name="ischeckbox"?checked="checked"?value="記住密碼"/>記住密碼</td>
?????????</tr>
??????????<tr>
???????????<td?colspan="2"?align="center"><input?type="submit"?name="submit"?value="登錄"/></td>
?????????</tr>
??????</table>
???</form>
<html>
<head>
<!--dologin.jsp--->
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<%@?page?import="java.net.*"?%>
<%@?page?language="java"?contentType="text/html;?charset=utf-8"
????pageEncoding="utf-8"%>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>???????</title>
</head>
<body>
<%?request.setCharacterEncoding("utf-8");
//判斷復(fù)選框是否被選中
String[]?checkbox=request.getParameterValues("ischeckbox");
if(checkbox!=null&&checkbox.length>0){
String?username=URLEncoder.encode(request.getParameter("username"),"utf-8");
String?password=URLEncoder.encode(request.getParameter("password"),"utf-8");
//獲得Cookie對(duì)象并將信息存入cookie對(duì)象
Cookie?usernameCookie=new?Cookie("username",username);
Cookie?passwordCookie=new?Cookie("password",password);
//保存cookie對(duì)象
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
usernameCookie.setMaxAge(864000);//生存期限10天
passwordCookie.setMaxAge(864000);
}
else{
Cookie[]?cookie=request.getCookies();//獲得之前保存過(guò)的Cookie對(duì)象
if(cookie!=null&&cookie.length>0){
for(Cookie?c:cookie){
if(c.getName().equals("username")||c.getName().equals("password")){
c.setMaxAge(0);//銷(xiāo)毀對(duì)象
response.addCookie(c);
}
}
}
}
%>
<a?href="testlogin.jsp"?>查看用戶(hù)信息</a>
</body>
</html>
testlogin.jsp
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<%@page?import="java.net.URLDecoder"%>
<html>
<head>
<%@?page?language="java"?contentType="text/html;?charset=utf-8"
????pageEncoding="utf-8"%>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>???????</title>
</head>
<body>
<h1>用戶(hù)信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String?username="";
String?password="";
Cookie[]?cookie=request.getCookies();//獲取Cookie對(duì)象
if(cookie!=null&&cookie.length>0){
for(Cookie?c:cookie){
if(c.getName().equals("username")){
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
用戶(hù)名:<%=username?%><br>
密碼:<%=password?%><br>
</body>
</html>
2017-08-02
去掉之后你需要重新提交一次,不然不會(huì)進(jìn)入dologin.jsp進(jìn)行判斷也就不會(huì)刪掉之前的信息,你只是把復(fù)選框去掉不提交可不還在嗎
2016-12-27
你設(shè)置的過(guò)期時(shí)間是10天呀,然后你之前已經(jīng)記住了,就算現(xiàn)在刪了,本地還是有這個(gè)信息的啊