Using JServ, even if a request is coming from the same browser, I am getting a new session each time. I am using request.getSession(true).getId() to find out session Id and it's different for every request. What's going wrong ?

Vikram Lele

request.getSession(true) will always return a new session.

You need to use request.getSession(false). If there is no session, it will return a null, in which case you need to create a new session.

Typically, assuming you have a login page, the session should be created in the login validation servlet, and all other servlets just need to do request.getSession(false), and if this returns a null, they should serve the login page.

If you don't have a login, you can do this:

  thisSession = request.getSession(false);
  if(thisSession == null)
     thisSession = request.getSession(true);
To make sure the session is properly maintained, you must call this method at least once before you write any output to the response.

- Vikram