Cookie相關

Hanyta發表於2024-05-07
  1. cookie的概念

    cookie是一種客戶端會話技術將資料儲存到客戶端。

  2. cookie的利弊:

    1)將資料儲存到客戶端,十分方便,同時減輕伺服器儲存壓力
    2)對儲存資料的個數(20)及大小有限制(4kb)

  3. 作用

    cookie用來儲存少量及不太敏感的資料
    可以在不登入的情況下,對客戶端完成身份識別(即在客戶端設定一個set-cookie的鍵值對,在訪問的時候使用鍵值對來獲取客戶端的身份)

  4. 使用Cookie

    1)建立cookie物件
    2)傳送cookie物件
    3)獲取cookie物件

    由ServletDemo01設定cookie,ServletDemo02獲取cookie

      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.Cookie;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      @WebServlet("/demo01")
      public class ServletDemo01 extends HttpServlet {
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //建立cookie
              Cookie cookie = new Cookie("name", "zhangsan");
              resp.addCookie(cookie);
          }
    
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              this.doPost(req, resp);
          }
      }
    
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.Cookie;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      @WebServlet("/demo02")
      public class ServletDemo02 extends HttpServlet {
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //獲取cookie
           Cookie[] cookies = req.getCookies();
           if (cookies!=null){
               for (Cookie cookie : cookies) {
                   //獲取cookie值與名稱
                   String value = cookie.getValue();
                   String name = cookie.getName();
                   System.out.println(name+":"+value);
               }
           }
          }
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              this.doGet(req, resp);
          }
      }
    
    
  5. 其他問題

    如何實現傳送多個Cookie:

      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //建立cookie
              //建立第一個cookie
              Cookie cookie = new Cookie("name1", "zhangsan");
              //傳送第一個cookie
              resp.addCookie(cookie);
              //建立第二個cookie
              Cookie id = new Cookie("name2", "lisi");
             //傳送第二個cookie
              resp.addCookie(id);
          }
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              this.doPost(req, resp);
          }
    

    Cookie在瀏覽器中的儲存多長時間:

    預設情況下瀏覽器關閉後,Cookie資料被銷燬
    可以呼叫setmaxage(int seconds)方法來設定Cookie的儲存時間

    • second為正數,將Cookie持久化寫入硬碟,並且指定儲存的時間,時間到了cookie自動失效
    • second為零,刪除cookie
    • second小於零,預設值,即瀏覽器關閉後刪除cookie

    Cookie中如何儲存中文:

    在tomcat 8 之前 cookie中不能直接儲存中文資料。
    需要將中文資料轉碼--一般採用URL編碼
    在tomcat 8 之後,cookie支援中文資料。
    特殊字元還是不支援,建議使用URL編碼儲存,URL解碼解析

     //URL編碼
     name= URLEncoder.encode(name,"utf-8");
     Cookie cookie = new Cookie("name", name);
     resp.addCookie(cookie);
     //解碼
     System.out.println(name+":"+ URLDecoder.decode(value,"utf-8"));