2008年10月20日 星期一

[Java][Fw]Java讀寫cookie

Java讀寫cookie

/*
* 寫cookie
*/
Cookie namecookie = new Cookie("name",name);
Cookie passwordcookie = new Cookie("password",password);
Cookie optioncookie = new Cookie("option","1");

//生命週期
namecookie.setMaxAge(60*60*24*365);
passwordcookie.setMaxAge(60*60*24*365);
optioncookie.setMaxAge(60*60*24*365);

response.addCookie(namecookie);
response.addCookie(passwordcookie);
response.addCookie(optioncookie);

/*
* 讀cookie
*/
Cookie[] cookies = request.getCookies();
if(cookies!=null)
{
String name = "";
String password = "";
String option = "";
for (int i = 0; i < cookies.length; i++)
{
Cookie c = cookies[i];
if(c.getName().equalsIgnoreCase("name"))
{
name = c.getValue();
}
else if(c.getName().equalsIgnoreCase("password"))
{
password = c.getValue();
}
else if(c.getName().equalsIgnoreCase("option"))
{
option = c.getValue();
}
}
}

[REF]http://www.blogjava.net/action/archive/2007/04/10/109574.html

沒有留言:

張貼留言