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" >十天內(nèi)記住登陸信息</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>
? ? <%
? ? //首先判斷用戶是否選擇了記住登陸狀態(tài)
? ? 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);
? ? //設(shè)置最大生存期限為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>
2017-02-09
doLogin.jsp 是否記住 用戶名密碼的字段用錯了。
? ? String[] strs=request.getParameterValues("inuseCooki");這句改成下面這樣:
?String[] strs=request.getParameterValues("isuseCooki");