「Java」Cookieの作成、読み書きメモ
1.cookieの作成
cookie= new Cookie(“cookiename","cookievalue");
cookie.setMaxAge(3600);
cookie.setPath(“/");
response.addCookie(cookie);
2.cookieの読み取り
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies){
cookie.getName();//get the cookie name
cookie.getValue();//get the cookie value
}
3.cookieの追加
public static void addCookie(HttpServletResponse
response,String name,String value,int maxAge){
Cookie cookie = new Cookie(name,value);
cookie.setPath(“/");
if(maxAge>0)
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
4.Cookieのnameとvaluwを取得
public static Cookie getCookieByName(HttpServletRequest request,String name){
Map<String,Cookie>cookieMap = ReadCookieMap(request);
if(cookieMap.containsKey(name)){
Cookie cookie = (Cookie)cookieMap.get(name);
return cookie;
}else{
return null;
}
}