Pr.Pg Next Pg

Deleting Session Data tutorials

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

  • Remove a particular attribute: Use the javax.servlet.http.HttpSession.getAttribute(String attribueName) method to access the attribute and use the removeAttribute(String attributeName) method to remove the attribute from the session.

  • 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.

  • Delete the whole session: You can call public void invalidate() method to delete an entire session.

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

  • web.xml Configuration: If you are using Tomcat, apart from the above mentioned methods, you can configure session time out in web.xml file as follows.




  • The timeout is expressed as minutes, and overrides the default timeout which is 30 minutes in Tomcat.

  • The getMaxInactiveInterval( ) method in a servlet returns the timeout period for that session in seconds. So if your session is configured in web.xml for 15 minutes, getMaxInactiveInterval( ) returns 900.



Example: Servlet program to deleting a session

Sam Sir

//Program name

// Program to deleting a session


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;



public class InvalidateSessionServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


HttpSession session = request.getSession();


session = request.getSession(false);

response.getWriter().println("Session : " + session);


protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);




Deleting a Session

Pr.Pg border                                              Next Pg