版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41690324/article/details/83820892
客户端的会话技术:cookie
A.Cookie API
1.创建Cookie对象.
Cookie cookie=new Cookie(String name,String value);
2.获取cookie名称
cookie.getName();
3.获取cookie值
cookie.getValue();
4.设置cookie为持久化cookie。
setMaxAge
cookie默认是会话级别,它保存在浏览器的缓存中,当关闭浏览器后,缓存中的内容清空。
如果cookie使用了setMaxAge进行设置,这时cookie就是持久化的cookie。当浏览器关闭,
持久化的cookie还存在。当重新打开浏览器,在发送请求,会将持久化的cookie.
带到服务器端.前提:cookie没有过期.
如果要删除cookie持久化文件,只需要设置setMaxAge(0) 如果值为负数,那么会在浏览器
关闭后删除cookie。
5.用于设置当前的cookie在什么样的uri访问时才会被带到服务器端.
setPath
练习时可以简单写在 setPath("/");
B.关于将cookie带回到浏览器端.
response.addCookie(Cookie c);
C.在服务器端得到cookie
Cookie[] cs=request.getCookies();
商品浏览记录实例
<%@ page language="java" import="java.util.*" pageEncoding="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>显示商品</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>
<a href="/HelloWeb/cookie/history.jsp">查看浏览记录</a>
<hr>
<a href="/HelloWeb/Goods?id=1">西游记</a>
<br>
<a href="/HelloWeb/Goods?id=2">红楼梦</a>
<br>
<a href="/HelloWeb/Goods?id=3">水浒传</a>
<br>
<a href="/HelloWeb/Goods?id=4">三国演义</a>
<br>
</body>
</html>
<%@page import="demo.utils.CookieUtils"%>
<%@ page language="java" import="java.util.*" pageEncoding="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>显示商品浏览记录</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>
<%
String[] books = { "西游记", "红楼梦", "水浒传", "三国演义" };
//得到history这个cookie
Cookie history = CookieUtils.getCookieByName(request.getCookies(),
"history");
if (history != null) {
//得到history的vlaue值
String value = history.getValue();
String[] ids = value.split("-");
for (int i = 0; i < ids.length; i++) {
int id = Integer.parseInt(ids[i]) - 1;
%>
<%=books[id]%><br>
<%
}
} else {
out.print("无浏览记录");
}
%>
</body>
</html>
package demo.cookie;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import demo.utils.CookieUtils;
public class GoodsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 得到请求的参数
String id = request.getParameter("id");
// 得到history名称的cookie
Cookie[] cs = request.getCookies();
Cookie history = CookieUtils.getCookieByName(cs, "history");
String ids = null;
// 如果history为null,说明第一次访问
if (history == null) {
ids = id;
} else {
// 不是第一次
// 得到cookie中的value
ids = history.getValue();
// 考虑重复
if (!Arrays.asList(ids.split("-")).contains(id)) {
ids += "-" + id;
}
}
// 存储到cookie中
history = new Cookie("history", ids);
// 持久化
// history.setMaxAge(30);
// 再返回到响应中
response.addCookie(history);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}