3 Answers

0 votes

When you are done with a user's session data, you have several options:

  • Remove a particular attribute: You can call public void removeAttribute(String name) method to delete the value associated with a particular key.

  • Delete the whole session: You can call public void invalidate() method to discard an entire session. Setting Session timeout: You can call public void setMaxInactiveInterval(int interval) method to set the timeout for a session individually.

  • Log the user out: The servers that support servlets 2.4, you can call logout to log the client out of the Web server and invalidate all sessions belonging to all the users.

answered Jun 30, 2016 by Nani
0 votes

To delete a session you can either call session.invalidate() method or session.setMaxInactiveInterval(0)

answered Mar 17 by Roshan333
0 votes

If you mean this by manually using removeAttribute() for every single attribute which can be obtained by getAttributeNames(), then this makes really no sense. I'm not sure whether it's the course/tutor which is bad or that you misinterpreted the course/tutor.


So one of my classmate asked - "How about we delete the HttpSession permanently after we've done using it?"

Yes, you can "delete" it by invalidating it.

session.invalidate();
response.sendRedirect("login.jsp");

Any subsequent request will force the server to create a new session. The redirect is by the way not necessary, but mandatory if you'd like to present the view in a fresh new session.

answered Apr 21 by LogenRahul

Related questions

How to delete a cookie using servlet?

There is no direct method offered by servlet API to delete a cookie on server side.Loop through the cookies in the request object to locate the one that you want to delete.for (Cookie cookie:...

solved
java, servlet, jdbc 2 Answer 140 views asked Jun 30, 2016 in Java by Naveen

How to read a cookie using servlet?

To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest. Then cycle through the array,...

solved
java, servlet, jdbc 2 Answer 63 views asked Jun 30, 2016 in Java by Nani