Thursday 29 April 2010

HttpSession

Session Management:

  • we are facing some problems with cookies, URL Rewriting and Hidden form fields to maintain the client state with sessions
  • The servlet container provides service (Session management) by implementing HttpSession interface
  •  In this concept, Web container stores the user's data and track the user's session by using URL Rewriting or Cookies
  • this mechanism helps to maintain a unique identity for each of session without using URL Rewriting or Cookies
Using HttpSession in Servlets involves the following functionality:
  1. it creates a new session object if necessary
  2. locating the session object associated with the current request
  3. storing information in a session
  4. attaching the session identity to the URL
  5. Looking up information in a session
  6. Discarding completed or abandoned sessions
Creating a Session Object:

  • A session object is created if the client does not carry the session identity or the client has not yet selected to join the session.
  • isNew() method of the the current request and  true if the session is created in the current request and returns false if it is a pre-existing session.


request.getSession(false) returns the existing session object... otherwise returns null.... Suppose in our above example.. When you call the request.getSession() for the first time it will give HttpSession object and if you use request.getSession(false) it will return null since server not yet created the HttpSession object yet... Once the request.getSession is made from then onwards... if you call request.getSession(false) it will return valid HttpSession object because already HttpSession object is created by that time

Example program for HttpSession

No comments:

Post a Comment