jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes problematic session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problematic session" Watch "problematic session" New topic

problematic session

Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
I�m using a HttpSession object to pass values in a servlet, the problem is tha in most pcs it�s working fine, but in some of them a new session is created and i loose all the values in the session i�ve enabled the cookies in these pcs and i�m still having this problem. Someone could please helpme. I really cant understand what�s going on.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Marcos,
Please check your code.See if you are creating a new instance of HttpSession.Check if you have

anywhere in the code.If you have, you have created a new session, which may have resulted in the loss of the name/value pair.
Hope this helps.
Sandeep Desai

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Yes I�m actually doing this at the doGet method, but how can I create a session if i don�t do this, and I�ve tryed to use the method that take no parameters as well (HttpSession session = req.getSession()) but i had the same problem.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I don't think request.getSession(true) will create a brand new session every timr. What the 'true' argumnet means is , return the session object if one already exists, ELSE create a new one and return.
So if you call 2 times with getSession(true), first time it will create a new session (assuming there is no prior session associated with user), next time it will return the old session which is already associated with the user.
'false' arg means, half workload of 'true' argument. It will return the session if one already exists,or else 'null'. It will not create a fresh one if there is no prior session.
Please refer to this API doc.

I know this doesn't answer your question. To find out if the returned session is indeed new or old, just add a code like this and check.
HttpSession sessn = request.getSession(true);
System.out.println(" It is Brand New session...");
System.out.println(" No...It is old session...");
maha anna
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Ok, I�ve read in the servlet specification v2.3 and the servlet documentation, and that�s why I�m mad about it, there they say about getSession in the servlet documentation HttpServletRequest class...
public HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
If create is false and the request has no valid HttpSession, this method returns null.
To make sure the session is properly maintained, you must call this method before the response is committed.
true - to create a new session for this request if necessary; false to return null if there's no current session
the HttpSession associated with this request or null if create is false and the request has no valid session
but form me this is not happening.
And maha anna, I�m printing in the servlet output the "name" of the session using the HttpSession method getId(), and in most machines I don�t have a problem, the session don�t changes, but in a few ones the session changes, I�ve enabled cookies in these machines ass well but the session keeps changing.
I�ve read about using encodeRedirectUrl() and tomorrow when I get to work I�m gonna try this.
Coul�d someone plz tell me if this is a good way???
Thank you all for the atention.
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
I�d like a suggestion about it.
Anyone could help me please?
Thanks in advance.
I agree. Here's the link: http://aspose.com/file-tools
subject: problematic session
It's not a secret anymore!