Tutorials4u.net Forum
Delete (or expire) a Cookie from a servlet - Printable Version

+- Tutorials4u.net Forum (http://www.tutorials4u.net/forum)
+-- Forum: Discussion Forum (/forumdisplay.php?fid=1)
+--- Forum: Servlets (/forumdisplay.php?fid=4)
+--- Thread: Delete (or expire) a Cookie from a servlet (/showthread.php?tid=20)


Delete (or expire) a Cookie from a servlet - tutorials4u - 12-14-2010 08:50 AM

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) {

Cookie cookie = new Cookie ("myCookie", "theCookieValue");

// Expire in five minutes (5 * 60)
cookie.setMaxAge( 300 );

// Expire after the browser is closed
// cookie.setMaxAge( -1 );

// Expire right now
// cookie.setMaxAge( 0 );

}

public void doGet(HttpServletRequest req, HttpServletResponse res) {
doPost(req, res);
}

}