转自: https://blog.csdn.net/u011024652/article/details/52015784
下面通过一个实例来介绍Cookie这些方法的使用。主要功能是用户登录,在用户登录时提示用户是否记住登录信息,当用户选择记住登录信息时,会将用户的登录信息保存在Cookie中,以后用户再进入登录页面时会自动为用户写入用户的登录信息。
一、HTTP协议的无状态性
因为HTTP协议是无状态的,也就是说当用户发送一次请求给服务器之后,服务器响应客户端的请求,当同一个客户端再次发送请求给服务器时,服务器并不知道这个请求是同一个客户端发送的。而要保存用户的状态,涉及到两种技术,Session和Cookie,其中Session是保存在服务器端的,会随着会话的结束而销毁,而Cookie保存在客户端,可以保存较长一段时间,但是就因为Cookie保存在客户端,因此有不安全的风险。
二、JSP中Cookie的使用
JSP中的涉及到Cookie常见的使用有以下几种:
Cookie cookie = new Cookie(String key, String value) | 创建一个新的Cookie对象 |
response.addCookie(cooke) | 写入Cookie对象 |
Cookie[] cookies = request.getCookies() | 读取Cookie对象 |
void setMaxAge(int expiry) | 设置Cookie有效期,以秒为单位 |
void setValue(String value) | Cookie创建后,对Cookie进行赋值 |
String getName() | 获取Cookie名称 |
String getValue() | 获取Cookie的值 |
int getMaxAge() | 获取Cookie的有效时间,以秒为单位 |
首先写一个登录页面login.jsp,里面涉及一个表单包含用户名和密码,同时让用户选择是否记住用户名密码,并检查用户的Cookie,看是否有用户的登录信息,如果有的话直接将Cookie中的信息填入登录页面中的相应位置。
- <%@ 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对象
- Cookie[] cookies = request.getCookies();
- if(null != cookies && cookies.length > 0) {
- for(Cookie c : cookies) {
- if(“userName”.equals(c.getName())) {
- //为了防止读取到的cookie值为乱码,
- //将cookie的值使用URLDecoder.decode(String str, String encoding)进行转码
- userName = URLDecoder.decode(c.getValue(), ”UTF-8”);
- } else if(“password”.equals(c.getName())) {
- password = URLDecoder.decode(c.getValue(), ”UTF-8”);
- }
- }
- }
- %>
- <form action=”dologin.jsp” action=“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=“isRemeber” value=“remeber” checked=“checked”>十天内免登录</td>
- </tr>
- <tr>
- <td colspan=”2”><input type=“submit” value=“登录”></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
<%@ 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对象
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0) {
for(Cookie c : cookies) {
if("userName".equals(c.getName())) {
//为了防止读取到的cookie值为乱码,
//将cookie的值使用URLDecoder.decode(String str, String encoding)进行转码
userName = URLDecoder.decode(c.getValue(), "UTF-8");
} else if("password".equals(c.getName())) {
password = URLDecoder.decode(c.getValue(), "UTF-8");
}
}
}
%>
<form action="dologin.jsp" action="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="isRemeber" value="remeber" checked="checked">十天内免登录</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
在这个页面中我们看到取出Cookie中保存的内容时用到了一个URLDecoder.decode(String str, String encoding)方法,这是因为根据RFC 2109中的规定,在Cookie中只能包含ASCII的编码,中文很显然不是ASCII编码的,为了防止出现乱码,需要进行一次转码,而要将中文存入Cookie的方法将在下面看到。
用户在登录页面写入相关信息并提交之后交给dologin.jsp进行处理,主要是根据用户的选择判断是否将用户的登录信息存入Cookie中,如果需要,则将登录信息写入Cookie。
- <%@ 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 = ”“;
- String[] isRemeber = request.getParameterValues(”isRemeber”);
- if(null != isRemeber && isRemeber.length > 0) {
- //获取userName和password的值,为了防止给cookie赋值时出现乱码异常,
- //使用URLEncoder.encode(String str, String encoding)转码
- userName = URLEncoder.encode(request.getParameter(”userName”), “UTF-8”);
- password = URLEncoder.encode(request.getParameter(”password”), “UTF-8”);
- //定义cookie
- Cookie userNameCookie = new Cookie(“userName”, userName);
- Cookie passwordCookie = new Cookie(“password”, password);
- //设置cookie的有效时间
- userNameCookie.setMaxAge(60 * 60 * 24 * 10);
- passwordCookie.setMaxAge(60 * 60 * 24 * 10);
- //写入cookie对象
- response.addCookie(userNameCookie);
- response.addCookie(passwordCookie);
- } else {
- Cookie[] cookies = request.getCookies();
- if(null != cookies && cookies.length > 0) {
- for(Cookie c : cookies) {
- if(“userName”.equals(c.getName()) || “password”.equals(c.getName())) {
- c.setMaxAge(0);
- response.addCookie(c);
- }
- }
- }
- }
- %>
- <a href=”testCookie.jsp”>测试Cookie是否保存</a>
- </body>
- </html>
<%@ 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 = "";
String[] isRemeber = request.getParameterValues("isRemeber");
if(null != isRemeber && isRemeber.length > 0) {
//获取userName和password的值,为了防止给cookie赋值时出现乱码异常,
//使用URLEncoder.encode(String str, String encoding)转码
userName = URLEncoder.encode(request.getParameter("userName"), "UTF-8");
password = URLEncoder.encode(request.getParameter("password"), "UTF-8");
//定义cookie
Cookie userNameCookie = new Cookie("userName", userName);
Cookie passwordCookie = new Cookie("password", password);
//设置cookie的有效时间
userNameCookie.setMaxAge(60 * 60 * 24 * 10);
passwordCookie.setMaxAge(60 * 60 * 24 * 10);
//写入cookie对象
response.addCookie(userNameCookie);
response.addCookie(passwordCookie);
} else {
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0) {
for(Cookie c : cookies) {
if("userName".equals(c.getName()) || "password".equals(c.getName())) {
c.setMaxAge(0);
response.addCookie(c);
}
}
}
}
%>
<a href="testCookie.jsp">测试Cookie是否保存</a>
</body>
</html>
在这里可以看到为了防止将数据存入Cookie中出现乱码,使用了URLEncoder.encode(String str, String encoding)进行转码。在这个页面中还添加了一个超链接,用于检查是否将用户信息存入了Cookie。下面写这个testCookie.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>测试Cookie</title>
- </head>
- <body>
- <h1>用户信息</h1>
- <hr/>
- <%
- request.setCharacterEncoding(”UTF-8”);
- String userName = ”“;
- String password = ”“;
- Cookie[] cookies = request.getCookies();
- if(null != cookies && cookies.length > 0) {
- for(Cookie c : cookies) {
- if(“userName”.equals(c.getName())) {
- userName = URLDecoder.decode(c.getValue(), ”UTF-8”);
- } else if(“password”.equals(c.getName())) {
- password = URLDecoder.decode(c.getValue(), ”UTF-8”);
- }
- }
- }
- %>
- 用户名:<%=userName %><br/>
- 密码:<%=password %><br/>
- </body>
- </html>
<%@ 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>测试Cookie</title>
</head>
<body>
<h1>用户信息</h1>
<hr/>
<%
request.setCharacterEncoding("UTF-8");
String userName = "";
String password = "";
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0) {
for(Cookie c : cookies) {
if("userName".equals(c.getName())) {
userName = URLDecoder.decode(c.getValue(), "UTF-8");
} else if("password".equals(c.getName())) {
password = URLDecoder.decode(c.getValue(), "UTF-8");
}
}
}
%>
用户名:<%=userName %><br/>
密码:<%=password %><br/>
</body>
</html>
全部写完后,运行程序,可以发现当用户填写了登录信息并选择保存十天后,信息被存在Cookie中,当用户关闭了浏览器,然后在Cookie的有效期内在进入登录页面时,页面会自动填入用户的登录信息。
</div>