Vorrei sapere come eliminare un cookie in uno HttpServletResponse
in Spring MVC. Ho il metodo di accesso in cui creo il cookie e il logout dove voglio eliminarlo, ma non funziona.Elimina cookie da una risposta servlet
Ecco il codice:
@RequestMapping(method = RequestMethod.POST)
public ModelAndView Login(HttpServletResponse response, String user, String pass) {
if (user != null && pass != null && userMapper.Users.get(user).getPass().equals(pass)){
Cookie cookie = new Cookie("user", user);
cookie.setPath("/MyApplication");
cookie.setHttpOnly(true);
cookie.setMaxAge(3600);
response.addCookie(cookie);
Map model = new HashMap();
model.put("user", user);
return new ModelAndView("home", "model", model);
}
return new ModelAndView("login");
}
@RequestMapping(value="/logout", method = RequestMethod.POST)
public ModelAndView Logout(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for(int i = 0; i< cookies.length ; ++i){
if(cookies[i].getName().equals("user")){
//Cookie cookie = new Cookie("user", cookies[i].getValue());
//cookie.setMaxAge(0);
//response.addCookie(cookie);
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
break;
}
}
return new ModelAndView("login");
}
ho pensato che era necessario solo cambiare il maxAge
, ma nel browser il cookie non cambiano. Ho anche provato a riscrivere un cookie con lo stesso nome nel blocco commentato ma non funziona neanche.
hai provato cookie.setvalue ("") ?? –
possibile duplicato di [Come si rimuove un cookie in un servlet Java] (http://stackoverflow.com/questions/890935/how-do-you-remove-a-cookie-in-a-java-servlet) – skaffman
sì, il setValue ("") non ha funzionato neanche ... – Cruz