cookie只记住了密码,没记住用户名是什么情况?
我的代码是这样的
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Login</h1>
<hr>
<%
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("usename")) {
username = c.getValue();
}
if (c.getName().equals("password")) {
password = c.getValue();
}
}
}
%>
<form action="dologin.jsp" method="post" name="loginForm">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" value="<%=username%>"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" value="<%=password%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>10days</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login"></td>
</table>
</form>
</body>
</html>
-----------------------
dologin.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Success</h1>
<hr>
<%
//remenber or not
String[] isUseCookies = request.getParameterValues("isUseCookie");
if (isUseCookies != null && isUseCookies.length > 0) {
//create username and password in cookies
String username = request.getParameter("username");
String password = request.getParameter("password");
Cookie usernameCookie = new Cookie("username", username);
Cookie passwordCookie = new Cookie("password", password);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
//10days
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);
} else {
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("usename")
|| c.getName().equals("password")) {
c.setMaxAge(0);
response.addCookie(c);
}
}
}
}
%>
<a href="users.jsp">Check User Info</a>
</body>
</html>
---------------------------
users.jsp
<!DOCTYPE HTML><%@page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Users</h1>
<%
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("usename")) {
username = c.getValue();
}
if (c.getName().equals("password")) {
password = c.getValue();
}
}
}
%>
Username=<%=username%><br> Password=<%=password%><br>
</body>
</html>
------------------
结果是密码有cookie但是用户名一直是空的