日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

web中的cookie管理

發布時間:2023/12/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 web中的cookie管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  本篇是以JSP為背景介紹,但是在web開發中也是相同的原理。

  什么是cookie

  由于http是一種無狀態的協議,因此服務器收到請求后,只會當做一次新的請求。即便你重復發送了1000次同樣的請求,這1000次都屬于獨立的請求。

  這樣顯然效率很低,如果要登錄某個網站,后期的操作都與用戶身份有關,難道還得沒操作一個頁面都得登錄一次?

  于是cookie和session就誕生了。

  cookie和session都是用于幫助http進行狀態管理的一種手段。

  cookie與session的區別

  cookie與session的區別可以通過下面幾點區分:

  1 保存位置:cookie保存在客戶端瀏覽器中;session保存在服務器端。

  2 生命周期:cookie由用戶指定或者使用默認的過期時間,在這段期限內cookie都保存在客戶端本地;session屬于一次會話,如果會話關閉,瀏覽器關閉,服務器啟動都會導致session的清除。

  3 數據類型:cookie其實就是一堆字符串;session是某種Object對象。

  4 安全性:cookie一般只保存一些用戶的行為習慣等等,像用戶名密碼肯定都需要經過加密的,即使泄露了也無關緊要;session則保存用戶相關的重要內容。

  cookie的使用過程

  如果要保存cookie

  首先需要創建一個Cookie對象,然后通過把它添加到response對象中,返回給客戶端即可。

  Cookie對象中的數據就自動保存在客戶端了。

  如果要使用cookie

  可以通過request對象直接查詢cookie信息,并且比對是否含有自己使用的數據。

  Cookie中常用的方法

  1 創建Cookie對象

Cookie usernameCookie = new Cookie("username",username);

  2 設置過期時間,以秒為單位

usernameCookie.setMaxAge(864000);

  3 保存cookie

response.addCookie(usernameCookie);

  4 獲取cookie數據

Cookie[] cookies = request.getCookies();

  5 提取關鍵數據

Cookie[] cookies = request.getCookies(); if(cookies!=null && cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){response.addCookie(c);}} }

  JSP中cookie使用樣例

  業務場景:

  1 login.jsp登錄用戶名密碼,可以設置是否記錄cookie;如果之前登陸過,則自動填寫cookie中的信息。

  2 跳轉到doLogin.jsp界面,進行cookie的保存于清除。如果前一頁設置保存,則保存cookie信息;如果前一頁設置不保存,則清除信息。

  3 通過URL跳轉到users.jsp頁面,可以提取cookie中的相關信息。

  login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"import="java.net.*"pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用戶登錄</title> </head> <body><h1>用戶登錄</h1><hr><%request.setCharacterEncoding("utf-8");String username = "";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null && cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username = URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password = URLDecoder.decode(c.getValue(),"utf-8");}}}%><form name="loginForm" action="doLogin.jsp" method="post"><table><tr><td>username</td><td><input type="text" name="username" value=<%=username%>></input></td></tr><tr><td>password</td><td><input type="password" name="password" value=<%=password%>></input></td></tr><tr><td><input type="checkbox" name="isUseCookie" checked="true"/>記住登錄狀態</td></tr><tr><td colspan="2" align="center"><input type="submit" value="submit"/></td></tr></table></form> </body> </html> View Code

  doLogin.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"import="java.net.*"pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用戶登錄</title> </head> <body><h1>javaBeans</h1><hr><%//保證request以及response的編碼 request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");String[] isUseCookies = request.getParameterValues("isUseCookie");if(isUseCookies!=null && isUseCookies.length>0 ){//使用URLEncoder解決cookie中中文問題String username = URLEncoder.encode(request.getParameter("username"),"utf-8");String password = URLEncoder.encode(request.getParameter("password"),"utf-8");Cookie usernameCookie = new Cookie("username",username);Cookie passwordCookie = new Cookie("password",password);usernameCookie.setMaxAge(864000);passwordCookie.setMaxAge(864000);response.addCookie(usernameCookie);response.addCookie(passwordCookie);}else{Cookie[] cookies = request.getCookies();if(cookies!=null && cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")||c.getName().equals("password")){c.setMaxAge(0);response.addCookie(c);}}}}%><a href="users.jsp" target="_blank">check user info</a> </body> </html> View Code

  users.jsp

<%@ page language="java" import="java.util.*,java.io.*,java.net.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body><h1>cookie</h1><%request.setCharacterEncoding("utf-8");String username = "";String password = "";Cookie[] cookies = request.getCookies();if(cookies!=null && cookies.length>0){for(Cookie c:cookies){if(c.getName().equals("username")){username = URLDecoder.decode(c.getValue(),"utf-8");}if(c.getName().equals("password")){password = URLDecoder.decode(c.getValue(),"utf-8");}}}%>用戶名:<%=username %>密碼:<%=password %> </body> </html> View Code

  其中關于編碼問題,可以參考:中文亂碼問題

轉載于:https://www.cnblogs.com/xing901022/p/4359732.html

總結

以上是生活随笔為你收集整理的web中的cookie管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。