DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • The Bill You Didn't See Coming
  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Mocking Kafka for Local Spring Development
  • Event-Driven Pipelines With Apache Pulsar and Go
  1. DZone
  2. Data Engineering
  3. Databases
  4. Destroy Cookie while Logging out.

Destroy Cookie while Logging out.

By 
Shiv Kumar Ganesh user avatar
Shiv Kumar Ganesh
·
Aug. 15, 13 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
41.3K Views

Join the DZone community and get the full member experience.

Join For Free

I was facing a problem where while a person logs out his session is invalidated but the JSESSIONID still remained in the browser. As a result while logging in the Java API used to get the request from the browser along with a JSESSIONID(Just the ID since the session was invalidated) and would create the new session with the same ID. To fix this problem I used the above code so that whenever a user logs out the entire JSESSIONID becomes empty and thus cookie wont exist for that site.Anyone using JAVA can utilize this in their code.

@RequestMapping(value = "/logout", method = RequestMethod.POST)
public void logout(HttpServletRequest request,
HttpServletResponse response) {
/* Getting session and then invalidating it */
HttpSession session = request.getSession(false);
if (request.isRequestedSessionIdValid() && session != null) {
session.invalidate();
}
handleLogOutResponse(response);
}

/**
 * This method would edit the cookie information and make JSESSIONID empty
 * while responding to logout. This would further help in order to. This would help
 * to avoid same cookie ID each time a person logs in
 * @param response
 */
private void handleLogOutResponse(HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
cookie.setMaxAge(0);
cookie.setValue(null);
cookie.setPath("/");
response.addCookie(cookie);
}
}
Session (web analytics) Java (programming language) Facing (retail) API Requests

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • The Bill You Didn't See Coming
  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook