//这是dologin.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h1>登录成功</h1>
<hr>
<br/>
<br/>
<br/>
<%
//首先判断用户是否选择了记住登录状态
String[] isUseCookies=request.getParameterValues("isUseCookie");
if(isUseCookies!=null&&isUseCookies.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(864000);
passwordCookie.setMaxAge(864000);//设置最大生存期限为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);//设置Cookies失效
response.addCookie(c);//重新保存
}
}
}
}
%>
<a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>
//这是user.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
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();
}
}
}
%>
<br/>
<br/>
<br/>
用户名:<%=username%><br/>
密码:<%=password%><br/>
</body>
</html>
//这是login.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
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();
}
}
}
%>
<br/>
<br/>
<br/>
用户名:<%=username%><br/>
密码:<%=password%><br/>
</body>
</html>