jsp状态管理
保存用户状态的两大机制:
1,session(详情见上章)
2,cookie
cookie概述:
是web服务器保存在客户端的一系列文本信息
典型应用一,判断已注册用户是否已经登陆网站
典型应用二,”购物车“的应用
生活中看视频的时候,系统会自动记录已经浏览过的视频
登陆QQ时,会自动记住账号和密码实现自动登陆的功能
弊端:容易泄露用户信息
cookie实现记忆用户名及密码的代码
login.jsp
<%@ page language="java" import="java.net.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>用户登陆</title>
</head>
<body>
<center>
<h1>用户登陆</h1>
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
Cookie [] cookies=request.getCookies();
if(cookies!=null || cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("username")){
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
<form action="success.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="isusecookie" checked="checked"/>十天内记住账号和密码</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</center>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
success.jsp
<%@ page language="java" import="java.net.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>登陆成功</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String[] iscookie=request.getParameterValues("isusecookie");
if(iscookie!=null && iscookie.length>0){
String username=URLEncoder.encode(request.getParameter("username"), "utf-8");;
String password=URLEncoder.encode(request.getParameter("password"), "utf-8");;
Cookie usernamecookie=new Cookie("username",username);//将用户名和密码保存在cookie中
Cookie passwordcookie=new Cookie("password",password);
usernamecookie.setMaxAge(864000);//设置用户名和密码的最长存在期限
passwordcookie.setMaxAge(864000);
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);//设置用户名和密码的存在期限为0
response.addCookie(c);//重新保存
}
}
}
}
%>
<h1>登陆成功</h1>
<hr>
<a href="select.jsp">查看用户信息</a>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
select.jsp
<%@ page language="java" import="java.net.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>查看用户信息</title>
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
Cookie [] cookies=request.getCookies();
if(cookies!=null || cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("username")){
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if(c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
用户名:<%=username %>
密码:<%=password %>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------
Session和Cookie的对比