Skip to content

Instantly share code, notes, and snippets.

@AppLoidx
Created August 11, 2019 11:25
Show Gist options
  • Save AppLoidx/5a79559a293361b6afc88598478208e4 to your computer and use it in GitHub Desktop.
Save AppLoidx/5a79559a293361b6afc88598478208e4 to your computer and use it in GitHub Desktop.
Deleting cookie in servlet
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteCookieServlet", urlPatterns = "/deleteCookie")
public class DeleteCookieServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        // To delete a cookie, we need to set
        // the max age of the cookie to 0 and then add it to the Servlet's
        // response method.
        
        Cookie cookie = new Cookie("username", "");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment