• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to delete a cookie on server in a J2EE application

September 25, 2008 //  by Krishna Srinivasan//  Leave a Comment

There is no direct method offered by servlet API to delete a cookie on server side. This is how you can go about doing it.

also read:

  • Java EE Tutorials
  • Servlets Interview Questions
  • New Features in Servlets 3.0
  • Asynchronous Servlets in Servlets 3
  1. Loop through the cookies in the request object to locate the one that you want to delete.
    for (Cookie cookie: request.getCookies()) {
        if (USER_COOKIE_NAME.equals(cookie.getName())) {
             return cookie;
        }
    }
  2. Set the maxAge property of the cookie to 0. This results in cookie expiring with immediate effect as soon as the client receives the response.
  3. Optionally, set the value property to empty string. This is not really required since the cookie is about to be expired. However, this adds a sense of a clean and complete erasure if the cookie being deleted carries some critical, secure data.
  4. Add this cookie to response. The cookie will be gone once the response is sent back to client!
    view plaincopy to clipboardprint
    cookie.setMaxAge(0);
    cookie.setValue("");
    response.addCookie(cookie);

 

Category: Java EETag: Servlets

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Advantages and Disadvantages – JSF
Next Post: EJB Webservices in JBoss application server sample code »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact