您的位置:首页 > 编程语言 > Java开发

java中对Cookie操作

2012-11-04 20:34 274 查看
public class CookieUtil {
private static String path = "/dang";
private static int age = 365*24*3600;
public static void addCookie(String name,String value,
HttpServletResponse response,int maxAge) throws UnsupportedEncodingException{
Cookie c = new Cookie(name,URLEncoder.encode(value,"utf-8"));
c.setMaxAge(maxAge);
c.setPath(path);
response.addCookie(c);
}
public static void addCookie(String name,String value,
HttpServletResponse response) throws UnsupportedEncodingException{
addCookie(name,value,response,age);
}

public static String findCookie(String name,HttpServletRequest request) throws UnsupportedEncodingException{
String value = null;
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if(cookie.getName().equals(name)){
value = URLDecoder.decode(cookie.getValue(),"utf-8");
}
}
}
return value;
}

public static void delete(String name,HttpServletResponse response){
Cookie c = new Cookie(name,"");
c.setMaxAge(0);
c.setPath(path);
response.addCookie(c);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: