Java 处理cookie的方法

 

 

一、java创建cookie

方法一:

Response.Cookies["userName"].Value = "patrick";

Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

方法二:

HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

 

 

二、java读取cookie

获取value的值:request.cookie[“name”].value=? 
 
if(Request.Cookies["userName"] != null)
    Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
 
if(Request.Cookies["userName"] != null)
{
    HttpCookie aCookie = Request.Cookies["userName"];
    Label1.Text = Server.HtmlEncode(aCookie.Value);
}

 

 

三、java销毁cookie

 

int counter;
if (Request.Cookies["counter"] == null)
    counter = 0;
else
{
    counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;
 
Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1); 



 

posted @ 2018-08-13 22:29  roychenyi  阅读(2259)  评论(0编辑  收藏  举报