Wednesday, 23rd June 2010
The new frontier for learning Java

Delete (or expire) a Cookie from a servlet

From WikiJava

Jump to: navigation, search


This example shows how to remove a cookie from a client's browser

Contents

the article

The example uses the Cookie.setMaxAge Method to set the expiration of a cookie. Max age zero tells the cookie to expire immediately.

MyServlet

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);
    }
 
}

See Also

Read a Cookie from a servlet, Set a Cookie from a servlet, Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: