cookie
为什么在user.jsp页面无法输出cookie,而且点记住登陆是无效的,明明和老师的代码完全一样啊!!!
login.jsp页面
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆界面</h1>
<hr>
<%
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username=cookie.getValue();
}
if (cookie.getName().equals("password")) {
password=cookie.getValue();
}
}
}
%>
<form name="loginForm" action="doLogin.jsp" method="post">
<table>
<tr>
<td>用户名:</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="isuseCooki" checked="checked" >十天内记住登陆信息</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
</body>
</html>
doLogin JSP页面
<%@page import="org.apache.taglibs.standard.tag.common.core.ForEachSupport"%>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doLogin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登陆成功</h1>
<hr>
<%
//首先判断用户是否选择了记住登陆状态
String[] strs=request.getParameterValues("inuseCooki");
if(strs!=null&&strs.length>0){
//把用户名和密码保存在cookie中
String username=request.getParameter("username");
String password=request.getParameter("password");
Cookie nameCookie=new Cookie("username",username);
Cookie wordCookie=new Cookie("password",password);
//设置最大生存期限为10天
nameCookie.setMaxAge(864000);
wordCookie.setMaxAge(864000);
response.addCookie(nameCookie);
response.addCookie(wordCookie);
}else{
Cookie [] cookies=request.getCookies();
if(cookies!=null&&cookies.length>0){
for(Cookie cookie:cookies){
if(cookie.getName().equals("username")||cookie.getName().equals("password")){
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
}
%>
<a href="user.jsp" >查看用户信息</a>
</body>
</html>
user.jsp页面
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'user.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username=cookie.getValue();
}
if (cookie.getName().equals("password")) {
password=cookie.getValue();
}
}
}
%>
用户名:<%=username %><br>
密码:<%=password %><br>
</body>
</html>