javaWeb学习日记_19:cookie

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31539817/article/details/84947027

1. Http协议与Cookie(了解)
  * Cookie是HTTP协议制定的!先由服务器保存Cookie到浏览器,再下次浏览器请求服务器时把上一次请求得到Cookie再归还给服务器
  * 由服务器创建保存到客户端浏览器的一个键值对!服务器保存Cookie的响应头:Set-Cookie: aaa=AAA  Set-Cookie: bbb=BBB
    > response.addHeader("Set-Cookie", "aaa=AAA");response.addHeader("Set-Cookie", "bbb=BBB");
  * 当浏览器请求服务器时,会把该服务器保存的Cookie随请求发送给服务器。浏览器归还Cookie的请求头:Cookie: aaa=AAA; bbb=BBB
  * Http协议规定(保证不给浏览器太大压力):
    > 1个Cookie最大4KB
    > 1个服务器最多向1个浏览器保存20个Cookie
    > 1个浏览器最多可以保存300个Cookie
  * 浏览器大战:因为浏览器竞争很激励,所以很多浏览器都会在一定范围内违反HTTP规定,但也不会让一个Cookie为4GB!

2. Cookie的用途
  * 服务器使用Cookie来跟踪客户端状态!
  * 保存购物车(购物车中的商品不能使用request保存,因为它是一个用户向服务器发送的多个请求信息)
  * 显示上次登录名(也是一个用户多个请求)

  **********Cookie是不能跨浏览器的!***********

3. JavaWeb中使用Cookie
  * 原始方式(了解):
    > 使用response发送Set-Cookie响应头
    > 使用request获取Cookie请求头
  * 便捷方式(精通):
    > 使用repsonse.addCookie()方法向浏览器保存Cookie
    > 使用request.getCookies()方法获取浏览器归还的Cookie

  Cookie第一例:
    > 一个jsp保存cookie, a.jsp
    > 另一个jsp获取浏览器归还的cookie! b.jsp

4. Cookie详解
  * Cookie不只有name和value两个属性
  * Cookie的maxAge(掌握):Cookie的最大生命,即Cookie可保存的最大时长。以秒为单位,例如:cookie.setMaxAge(60)表示这个Cookie会被浏览器保存到硬盘上60秒
    > maxAge>0:浏览器会把Cookie保存到客户机硬盘上,有效时长为maxAge的值决定。
    > maxAge<0:Cookie只在浏览器内存中存在,当用户关闭浏览器时,浏览器进程结束,同时Cookie也就死亡了。
    > maxAge=0:浏览器会马上删除这个Cookie!

a.jsp:显示Cookie的MaxAge<

<%@ 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>My JSP 'a.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>显示Cookie的MaxAge</h1>
<%-- request、response、session、application、pageContext、config、out、page、exception --%>
<%
	Cookie cookie1 = new Cookie("aaa", "AAA");
	cookie1.setMaxAge(60*60);
	response.addCookie(cookie1);
%>
  </body>
</html>

b.jsp:删除cookie

<%@ 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>My JSP 'b.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>删除cookie</h1>
<%
Cookie cookie1 = new Cookie("aaa", "AAA");
cookie1.setMaxAge(0);
response.addCookie(cookie1);
%>
  </body>
</html>


  * Cookie的path(理解):
    > Cookie的path并不是设置这个Cookie在客户端的保存路径!!!
    > Cookie的path由服务器创建Cookie时设置
    > 当浏览器访问服务器某个路径时,需要归还哪些Cookie给服务器呢?这由Cookie的path决定。
    > 浏览器访问服务器的路径,如果包含某个Cookie的路径,那么就会归还这个Cookie。
    > 例如:
      <> aCookie.path=/day11_1/; bCookie.path=/day11_1/jsps/; cCookie.path=/day11_1/jsps/cookie/;
      <> 访问:/day11_1/index.jsp时,归还:aCookie
      <> 访问:/day11_1/jsps/a.jsp时,归还:aCookie、bCookie
      <> 访问:/day11_1/jsps/cookie/b.jsp时,归还:aCookie、bCookie、cCookie
    > Cookie的path默认值:当前访问路径的父路径。例如在访问/day11_1/jsps/a.jsp时,响应的cookie,那么这个cookie的默认path为/day11_1/jsps/
  * Cookie的domain(了解)
    > domain用来指定Cookie的域名!当多个二级域中共享Cookie时才有用。
    > 例如;www.baidu.com、zhidao.baidu.com、news.baidu.com、tieba.baidu.com之间共享Cookie时可以使用domain
    > 设置domain为:cookie.setDomain(".baidu.com");
    > 设置path为:cookie.setPath("/");


Cookie中不能存在中文!!!

// 保存
Cookie c = new Cookie("username", URLEncoder.encode("张三", "utf-8"));//出错!
response.addCookie(c);

扫描二维码关注公众号,回复: 4479463 查看本文章

// 获取
Cookie[] cs = request.getCookies();
if(cs != null) {
  for(Cookie c : cs){
    if("username".equals(c.getName())) {
      String username = c.getValue();
      username = URLDecoder.decode(username, "utf-8");
    }
  }
}

例子:cookie的保存和获取

a.jsp:保存

<%@ 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>My JSP 'a.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>保存Cookie</h1>
<%-- request、response、session、application、pageContext、config、out、page、exception --%>
<%
	Cookie cookie1 = new Cookie("aaa", "AAA");
	response.addCookie(cookie1);
	
	Cookie cookie2 = new Cookie("bbb", "BBB");
	response.addCookie(cookie2);
%>
  </body>
</html>

b.jap:获取cookie

<%@ 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>My JSP 'b.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>获取Cookie</h1>
<%
	Cookie[] cookies = request.getCookies();
	if(cookies != null) {
		for(Cookie c : cookies) {
			out.print(c.getName() + "=" + c.getValue() + "<br/>");
		}
	}
%>
  </body>
</html>


 

猜你喜欢

转载自blog.csdn.net/qq_31539817/article/details/84947027
今日推荐