07-06
25
读写Cookie
作者:Java伴侣 日期:2007-06-25
/*
* 写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();
}
}
}
* 写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();
}
}
}
评论: 0 | 引用: 0 | 查看次数: 619
发表评论