title: Delete cookie from a servlet response
body: <p>I would like to know how to delete a cookie in an <code>HttpServletResponse</code> in Spring MVC. I have the login method where I create the cookie and the logout where I want to delete it, but it doesn't work.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
  public ModelAndView Login(HttpServletResponse response, String user, String pass) {     
      if (user != null &amp;&amp; pass != null &amp;&amp; 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&lt; 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");
   }
  </code></pre>
  
  <p>I thought it was only needed to change the <code>maxAge</code>, but in the browser the cookie don't change. I even tried to rewrite a cookie with the same name in the commented block but it doesn't work either.</p>