HttpServletRequest: getSession() : HttpServletRequest « javax.servlet.http « Java by API






HttpServletRequest: getSession()


import java.io.IOException;
import java.util.Date;

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 ManualInvalidate extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {
    res.setContentType("text/html");

    HttpSession session = req.getSession();

    // Invalidate the session if it's more than a day old or has been
    // inactive for more than an hour.
    if (!session.isNew()) { // skip new sessions
      Date dayAgo = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
      Date hourAgo = new Date(System.currentTimeMillis() - 60 * 60 * 1000);
      Date created = new Date(session.getCreationTime());
      Date accessed = new Date(session.getLastAccessedTime());

      if (created.before(dayAgo) || accessed.before(hourAgo)) {
        session.invalidate();
        session = req.getSession(); // get a new session
      }
    }
  }
}

           
       








Related examples in the same category

1.HttpServletRequest: getAuthType()
2.HttpServletRequest: getCookies()
3.HttpServletRequest: getHeaders(String name)
4.HttpServletRequest: getHeader( ) by Accept-Language
5.HttpServletRequest: getHeader( ) by Accept-Charset
6.HttpServletRequest: getRequestURI()
7.ServletRequest: getReader()
8.ServletRequest: getRequestDispatcher(String arg0)
9.HttpServletRequest: getHeaderNames()
10.HttpServletRequest: getPathInfo()
11.HttpServletRequest: getPathTranslated()
12.HttpServletRequest: getQueryString()
13.HttpServletRequest: getRemoteUser()
14.HttpServletRequest: getUserPrincipal()
15.HttpServletRequest: isRequestedSessionIdFromCookie()
16.HttpServletRequest: isRequestedSessionIdFromURL()
17.HttpServletRequest: isRequestedSessionIdValid()
18.HttpServletRequest: isUserInRole(String arg0)