What is HTTP Session in Servlets

Session tracking in Servlets is done by using Interface HttpSession. It helps to identify a client throughout many page requests or visiting a website and to store details about that client.

One of the recommended approaches is HTTP session. A request is being identified by the session which is originated from similar browser during the conversation time period. Same session could be shared by all servlets. The JSESSIONID gets generated by server and is passed via cookies to the client, Built in SSL mechanism or URL rewriting (if cookies get off). For minimizing the object’s size stored in session, care shall be taken.

To obtain the session in Java servlet proceed as following:

HttpSession session = request.getSession();

  • If user already has a session  the existing session is returned.
  • If no session exists a new one is created and returned.
  • If you want to know if this is a new session:

call the Session isNew() method.