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與session原理簡要CookieSession
- 新增cookie、刪除cookie、清除cookieCookie
- Cookie、Session、JWT在koa中的應用及實現原理CookieSessionJWT
- SYN Cookie原理及在Linux核心中的實現(轉)CookieLinux
- 會話與 Cookie:使用者登入的原理是什麼?會話Cookie
- Java操作Cookie之新增CookieJavaCookie
- 不用Cookie的“Cookie”技術Cookie
- CookieCookie
- 不使用 Cookie 的“Cookie”技術Cookie
- 如何設定cookie和刪除cookieCookie
- 如何獲取Cookie並使用Cookie侵入Cookie
- 操作cookie外掛jquery.cookie用法CookiejQuery
- go cookieGoCookie
- Cookie 起源Cookie
- javascript cookieJavaScriptCookie
- cookie & sessionCookieSession
- 新增cookieCookie
- cookie sessionCookieSession
- js cookieJSCookie
- cookie清除Cookie
- WebBrowser CookieWebCookie
- 操作cookieCookie
- Cookie安全Cookie
- jquery cookiejQueryCookie
- 設定cookie和查詢cookie的方法Cookie
- cookie外掛-jquery.cookie.js介紹CookiejQueryJS
- Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- Python中Cookie的處理(一)Cookie庫PythonCookie
- Cookie介紹及JavaScript操作Cookie方法詳解CookieJavaScript
- JavaScript 建立CookieJavaScriptCookie
- cookie 簡介Cookie
- Cookie簡介Cookie
- iOS - WKWebView CookieiOSWebViewCookie
- Cookie筆記Cookie筆記
- Cookie學習Cookie
- Cookie小記Cookie
- Postman同步cookiePostmanCookie