好像每次使用request.getParameter都没效果,这次代码在网页中没保存cookies!
login.jsp:
<body>
<%
String USERNAME = "";
String PASSWORD = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0){
for(Cookie c : cookies){
if(c.getName().equals("userName")){
USERNAME = c.getValue();
}
if(c.getName().equals("passWord")){
PASSWORD = c.getValue();
}
}
}
%>
<div class="classA">用户登录界面</div>
<form action="dologin.jsp" method="post">
<div>用户名:</div><input type="text" name="username" value=<%=USERNAME %>><br/>
<div>密码:</div><input type="password" name="password"><br/>
<div><input type="checkbox" name="isUseCookie" checked="checked" value=<%=PASSWORD %>>十天内记住我的登录状态</div>
<input type="submit" value="提交">
<input type="reset" value="重置">
</body>
dologin.jsp:
<body>
<div>登录成功欢迎</div>
<%
//判断用户是否选择了登录状态
String[] useCookies = request.getParameterValues("isUseCookie");
if(useCookies != null && useCookies.length > 0){
//把用户名和密码保存在Cookie对象里面
String userName = request.getParameter("username");
String passWord = request.getParameter("password");
Cookie userNameCookie = new Cookie("userName","userName");
Cookie passWordCookie = new Cookie("passWord","passWord");
userNameCookie.setMaxAge(60*60*24*10);//设置最大生存期限为10天
passWordCookie.setMaxAge(60*60*24*10);
response.addCookie(userNameCookie);
response.addCookie(passWordCookie);
} else{
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0){
for(Cookie c : cookies){
if(c.getName().equals("userName") && c.getName().equals("passWord")){
c.setMaxAge(0);//设置cookie失效
response.addCookie(c);//重新保存cookie
}
}
}
}
%>
<a href = "user.jsp" target="_blank">用户信息</a>
</body>
user.jsp:
<body>
<%
String USERNAME = "";
String PASSWORD = "";
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0){
for(Cookie c : cookies){
if(c.getName().equals("userName")){
USERNAME = c.getValue();
}
if(c.getName().equals("passWord")){
PASSWORD = c.getValue();
}
}
}
%>
<div>用户名:<%=USERNAME %></div><br/>
<div>密码:<%=PASSWORD %></div><br/>
</body>
哪里有问题啊?