javaWeb學習日記_19:cookie

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);

// 獲取
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>


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章