Cookie原理
1. 概念:客戶端會話技術,將資料儲存到客戶端
功能:在一次會話的範圍內的多次請求間,共享資料(一次會話可以多次請求和響應的)
2. 快速入門:
- 使用步驟:
- 建立Cookie物件,繫結資料
- new Cookie(String name, String value)
- 傳送Cookie物件(伺服器傳送給瀏覽器,然後瀏覽器就儲存改cookie,下次請求伺服器時(比如訪問ServletCookiedemo2),會攜帶這個cookie去請求伺服器)
- response.addCookie(Cookie cookie)
- 獲取Cookie,拿到資料(由於瀏覽器攜帶cookie來訪問伺服器的某個資源(eg:某個servlet類),所以用resquest獲取它請求的cookie資訊)
- Cookie[] request.getCookies()
定義兩個servlet類:
ServletCookiedemo1:
package cn.itcast.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;
/**
* @author QLBF
* @version 1.0
* @date 2020/11/24 19:10
*/
@WebServlet("/ServletCookiedemo1")
public class ServletCookiedemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.伺服器建立Cookie物件
Cookie c=new Cookie("msg","hellocookie");
//2.傳送Cookie給瀏覽器
response.addCookie(c);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
ServletCookiedemo2:
package cn.itcast.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;
/**
* @author QLBF
* @version 1.0
* @date 2020/11/24 19:10
*/
@WebServlet("/ServletCookiedemo2")
public class ServletCookiedemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//3. 獲取Cookie(獲取ServletCookiedemo1返回給伺服器的cookie)
Cookie[] cs = request.getCookies();
//獲取資料,遍歷Cookies
if (cs!=null){
for (Cookie c : cs) {
String name = c.getName();
String value = c.getValue();
System.out.println(name+":"+value);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
當瀏覽器訪問ServletCookiedemo1後,http://localhost:8080/bbb/ServletCookiedemo1(這裡虛擬目錄為bbb),再訪問http://localhost:8080/bbb/ServletCookiedemo2。控制檯會輸出:
這裡面可以獲取ServletCookiedemo1設定的鍵值,可以共享資料。
3. 實現原理
- 基於響應頭set-cookie和請求頭cookie實現
相關文章
- 說說劫持 Cookie 的原理Cookie
- 新增cookie、刪除cookie、清除cookieCookie
- Cookie、Session、JWT在koa中的應用及實現原理CookieSessionJWT
- Java操作Cookie之新增CookieJavaCookie
- CookieCookie
- 如何獲取Cookie並使用Cookie侵入Cookie
- 如何設定cookie和刪除cookieCookie
- cookie sessionCookieSession
- 新增cookieCookie
- go cookieGoCookie
- Cookie 起源Cookie
- javascript cookieJavaScriptCookie
- cookie & sessionCookieSession
- cookie外掛-jquery.cookie.js介紹CookiejQueryJS
- Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- Python Flask,cookie,設定、獲取、刪除cookiePythonFlaskCookie
- cookie跨域共享 cookie二級域名共享 前後端分離專案共享cookieCookie跨域後端
- Cookie小記Cookie
- Cookie學習Cookie
- iOS - WKWebView CookieiOSWebViewCookie
- Cookie筆記Cookie筆記
- cookie 和 sessionCookieSession
- cookie、sessionStorage、localStorageCookieSession
- Cookie Store APICookieAPI
- Cookie相關Cookie
- cookie 簡介Cookie
- JavaScript 建立CookieJavaScriptCookie
- Postman同步cookiePostmanCookie
- Cookie簡介Cookie
- session和cookieSessionCookie
- cookie&sessionCookieSession
- Session與CookieSessionCookie
- cookie, sessionStorage, localStorageCookieSession
- Cookie和SessionCookieSession
- Cookie 詳解以及實現一個 cookie 操作庫Cookie
- Python教程分享:Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- 初次接觸 CookieCookie
- cookie、session總結CookieSession