I set the max age of cookie, but the cookie still does not exist after closing browser. My controller:
package com.jiaotong114.jiaotong.controller;
@Controller
@RequestMapping("/")
public class CityIndex {
@RequestMapping(value="/city/{cityName}", method = RequestMethod.GET)
public String printHello(ModelMap model, @PathVariable("cityName") String cityName, HttpServletRequest request, HttpServletResponse response) {
Cookie[] c = request.getCookies();
boolean isNew = true;
for(int i = 0; i < c.length; i++) {
if(c[i].getName().equals("cityName")) {
c[i].setValue(cityName);
c[i].setMaxAge(365 * 24 * 60 * 60);
response.addCookie(c[i]);
isNew = false;
}
}
if(isNew) {
Cookie cityNameCookie = new Cookie("cityName", cityName);
cityNameCookie.setMaxAge(365 * 24 * 60 * 60);
response.addCookie(cityNameCookie);
}
request.getSession().setAttribute("cityName", cityName);
return "index";
}
}
Run this controller by accessing http://localhost:8080/city/shanghai.
Then the cookie added to the client.
image:
The expiry time is one year. (as I set in code: cityNameCookie.setMaxAge(365 * 24 * 60 * 60);
)
But when I closed the brower, reopened it and entered the http://localhost:8080. Then I found my cityName cookie disappear.
I set the max age time of cookie in the code, why did the cookie disappear after closing browser?