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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cookie的简单学习

發(fā)布時(shí)間:2025/6/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cookie的简单学习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Cookie的使用


一、cookie的作用

在我們平常寫的B/S程序中,會經(jīng)常用到cookie,主要有以下一些作用:

1、 記錄用戶名和密碼

以及該用戶需要保存的一些信息,如購物購站,使用cookie,可以讓用戶自動登錄到站點(diǎn)等。

2、 定制站點(diǎn)

可以使用cookie來記錄用戶的偏好。

3、 定向廣告

可以使用cookie來記錄用記經(jīng)常訪問的主題,并向他們顯示與這些主題相關(guān)的廣告。


二、向客戶程序發(fā)送cookie

1、 創(chuàng)建cookie對象

Cookie c = new Cookie(“userId”,”landril”);

2、 設(shè)置最大時(shí)效,默認(rèn)該cookie是存儲在瀏覽器的內(nèi)在中,用戶關(guān)閉瀏覽器則被刪除,下面的方法是將cookie存儲在硬盤上。

c.setMaxAge(60*60*24);//一天,如果設(shè)置為0則是刪除該cookie

3、 將cookie放入到HTTP響應(yīng)報(bào)頭,可以使用HttpServletResponse的addCookie方法,此方法不修改之前指定的Set-Cookie報(bào)頭,而是創(chuàng)建新的報(bào)頭。

response.addCookie(c);

? ? ? 注意:設(shè)置cookie的步驟為創(chuàng)建cookie對象,設(shè)置最大時(shí)效,將cookie放入響應(yīng)報(bào)頭,即發(fā)送到客戶程序,記住一定要將cookie發(fā)送到客戶程序


三、從客戶端讀取cookie

1、 調(diào)用HttpServletRequest的getCookies得到一個(gè)Cookie對象的數(shù)組

2、 對數(shù)組進(jìn)行循環(huán),調(diào)用cookie的getName方法,獲取具體的cookie的值

Cookie[] cookies = request.getCookies();

if(cookies != null){

for(int i=0;i<cookies.length;i++){

? ? ? ?Cookie c = cookies[i];

? ? ? ?if(“userId”.equals(c.getName())){

? ? ? ?System.out.println(c.getValue());

}

}

}


四、cookie的常用方法

1、 setComment()/getComment():指定或查找與該cookie相關(guān)的注釋

2、 setDomain()/getDomain():設(shè)置或讀取該cookie適用的域

3、 setMaxAge()/getMaxAge():操作cookie保留的時(shí)間,多長時(shí)間后過期

4、 getName():讀取cookie的名稱

5、 setPath()/getPath():設(shè)置或取得cookie適用的路徑

cookie.setPath(“/”);指定服務(wù)器的所有頁面都應(yīng)該收到該cookie

6、 setSource()/getSource():指定cookie是否只能通過加密連接(SSL)

默認(rèn)false,表示cookie適用所有連接

7、 setValue()/getValue():指定或獲取cookie的值


五、使用cookie


1、RepeatServlet.java

publicclass?RepeatServlet?extends?HttpServlet {


publicvoid?doGet(HttpServletRequest request, HttpServletResponse response)

throws?ServletException, IOException {

boolean?newa =?true;

? ? ? ?Cookie[] cookies = request.getCookies();

if(cookies !=?null){

for?(int?i = 0; i < cookies.length; i++) {

? ? ? ? ? ? ? Cookie c = cookies[i];

if?((c.getName().equals("repeat")) && (c.getValue().equals("true"))) {

? ? ? ? ? ? ? ? ?newa =?false;

break;

? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ? }


? ? ? ?String title;

if?(newa) {

? ? ? ? ? Cookie rtn =?new?Cookie("repeat"," true");

? ? ? ? ? rtn.setMaxAge(60*60*24*365);

? ? ? ? ? response.addCookie(rtn);

? ? ? ? ? title = "First Welcome";

? ? ? }?else?{

? ? ? ? ? title = "Welcom Back";

? ? ? }


? ? ? ?response.setContentType("text/html");

? ? ? ?PrintWriter out = response.getWriter();

? ? ? ?out.println("<html><body><h3>");

? ? ? ?out.println(title);

? ? ? ?out.println("</h3></body></html>");

? ?}


publicvoid?doPose(HttpServletRequest request, HttpServletResponse response)

throws?ServletException, IOException {

? ? ? ?doGet(request,response);

? ?}

}


2、CookieUtil.java

publicclass?CookieUtil{


publicstatic?String getValue(HttpServletRequest request,String cName,String value){

? ? ? ?Cookie[] cookies = request.getCookies();

if(cookies !=?null){

for?(int?i = 0; i < cookies.length; i++) {

? ? ? ? ? ? ? Cookie cookie = cookies[i];

if?(cName.equals(cookie.getName())) {

return?cookie.getValue();

? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ? }

return?value;

? ?}


publicstatic?Cookie getCookie(HttpServletRequest request,String cName){

? ? ? ?Cookie[] cookies = request.getCookies();

if(cookies !=?null){

for?(int?i = 0; i < cookies.length; i++) {

? ? ? ? ? ? ? Cookie cookie = cookies[i];

if?(cName.equals(cookie.getName())) {

return?cookie;

? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ? }

returnnull;

? ?}

}

您也可以下載一些例子,下載名稱:cookie_han


=============================================2013-06-17-han-add

里面包括java操作cookie的工具類,設(shè)置cookie,刪除cookie,以及得到cookie的值,你可以參考下,如果需要例子,您可以去下載,在tomcat中部署一下就可以用了!謝謝


package com.hanchao.util;


import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

* cookie工具類

* @author hanchao

* 2013-04-23

*/

public class CookieUtil {


/*******************

* 返回用戶訪問的次數(shù)

* hanchao

* 2013-04-23

* *****************

* @param request

* @param cName

* @param value

* @return

*/

public static String getValue(HttpServletRequest request,String cName,String value) {

Cookie[] cookies = request.getCookies();

if(cookies != null) {

for(int i = 0; i < cookies.length; i++) {

Cookie cookie = cookies[i];

if(cName.equals(cookie.getName())) {

return cookie.getValue();

}

}

}

return value;

}

/**************************

* get Cookie By cookieName

* hanchao

* 2013-04-23

* ***********************

* @param request

* @param cName

* @return

*/

public static Cookie getCookie(HttpServletRequest request,String cName) {

Cookie[] cookies = request.getCookies();

if(cookies != null) {

for(int i = 0; i < cookies.length; i++) {

Cookie cookie = cookies[i];

if(cookie != null && cName.equals(cookie.getName())) {

return cookie;

}

}

}

return null;

}

/**

* 添加cookie

* @param response

* @param namecookie的key值

* @param valuecookie的value值

* @param pathcookie的路徑

* @param domaincookie的域

* @param timeoutcookie的過期時(shí)間

* 2013-6-18

* @author: 韓超

*/

public static void addCookie(HttpServletResponse response,String name,String value,String path,String domain,int timeout) {

Cookie cookie = new Cookie(name, value);

if(domain == null) {

domain = ".baidu.com";//Constant.PASSPORTDOMAIN;

}

if(path == null) {

path = "/";

}

cookie.setDomain(domain);

cookie.setPath(path);

cookie.setMaxAge(timeout);

response.addCookie(cookie);

}

/**

* 刪除cookie

* @param request

* @param response

* @param namecookie的名稱

* 2013-6-18

* @author: 韓立偉

*/

public static void delCookie(HttpServletRequest request,HttpServletResponse response,String name) {

Cookie[] cookies = request.getCookies();

for(Cookie cookie : cookies) {

if(cookies != null && (name).equals(cookie.getName())) {

addCookie(response,name,null,null,null,0);

return;

}

}

}

/**

* 修改cookie的value值

* @param request

* @param response

* @param name

* @param value

* 2013-6-18

* @author: 韓超

*/

public static void updateCookie(HttpServletRequest request,HttpServletResponse response,String name,String value) {

Cookie[] cookies = request.getCookies();

for(Cookie cookie : cookies) {

if(cookies != null && (name).equals(c


ookie.getName())) {

addCookie(response,name,value,cookie.getPath(),cookie.getDomain(),cookie.getMaxAge());

return;

}

}

}

}



? ? ?本文轉(zhuǎn)自韓立偉 51CTO博客,原文鏈接:http://blog.51cto.com/hanchaohan/1185507,如需轉(zhuǎn)載請自行聯(lián)系原作者




總結(jié)

以上是生活随笔為你收集整理的cookie的简单学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。