Session Management with HttpSession

Chapter 4 - Session Management

    Session Management with HttpSession

  • Session Management with HttpSession

    Servlet API provides Session management through HttpSession interface and also get session from HttpServletRequest object using following methods.

    1. HttpSession getSession() – This method always returns a HttpSession object. It returns the session object attached with the request, if the request has no session attached, then it creates a new session and return it.

    2. HttpSession getSession(boolean flag) – This method returns HttpSession object if request has session else it returns null.

    Important methods of HttpSession are:

    1. String getId() – Returns a string containing the unique identifier assigned to this session.2. Object getAttribute(String name) – Returns the object bound with the specified name used to the Session attributes are getAttributeNames(), removeAttribute(String name) and setAttribute(String name, Object value).

    3. long getCreationTime() – Returns the time when the session was created, measured in milliseconds since midnight January 1, 1970 GMT. We can get last accessed time with getLastAccessedTime() method.

    4. setMaxInactiveInterval(int interval) – Specifies the time, in seconds, between client requests before the servlet container will invalidate the session.

    5. ServletContext getServletContext() – Returns ServletContext object for the application.

    6. boolean isNew() – Returns true if the client does not know about the session or if the client chooses not to join the session.

    7. void invalidate() – Invalidates this session then unbinds any objects bound to it.

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy