「Java入門」Cookieの設定、取得サンプル

1.cookieの設定
Cookie userCookie=new Cookie(“userInfo”,userInfo);
userCookie.setMaxAge(30*24*60*60); //一か月
userCookie.setPath(“/”);
response.addCookie(userCookie);

2.cookieの取得
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies){
if(cookie.getName().equals(“userInfo”)){
String userInfo = cookie.getValue();
String username = userInfo.split(“,”)[0];
String password = userInfo.split(“,”)[1];
String address = userInfo.split(“,”)[2];
request.setAttribute(“username”, username);
request.setAttribute(“password”, password);
request.setAttribute(“address”, address);
}
}

Comments are closed.