aspose file tools*
The moose likes Servlets and the fly likes Problems with sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems with sessions " Watch "Problems with sessions " New topic
Author

Problems with sessions

nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 41
I think that my problem is wierd and even though I searched the forum I couldnt find the solution. So here is my doubt regarding sessions. I am using servlets and JSP's for testing sessions. I am using Tomcat Server(IDK if different servers handle sessions in different ways). Every time I send a request from a jsp page which is the first page for my application.. it hits a servlet. So as this is my first page there should not be any session existing. So when I do request.getsession(); I get a session ID and when ever I try to check the properties of that session variable while debugging it shows that the session is not new. I invalidate all the sessions before calling the first page. But things are the same. Ultimately what I want over here is, when a user clicks my first page and enters his name it should store in sessions. And every time I display a page it should show his name in all other pages. I added the username that I got from the first page into the session variable "only if the session is new".. But unfortunately I could not create a new session.






I did session.invalidate() few hundred times before clicking my first page. But still my output is
59908F11DF53149C9DAD6A244CAC2EC7hello
welcome existing usernull


I tried all the ways but useless. At times I feel I want to maintain an arraylist for checking the sessions . Is there any method which creates a new session??? Really I feel frustrated. I have been trying to sort this out since more than a week . Can anyone help me???
nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 41
Sorry found the solution. It seems JPS have some kind of useless stuff............ So i was having that problem. By using this statement I could figure out my problem
<%@ page session="false" %>
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

if you are using session. try to set attributes to sessions for username. and check if it is null, invalidate the session.

hope this helps.

Cheers,
Alex Sales


OCPJP 6, OCEWCD Java EE 6
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
nithin chinni wrote:Sorry found the solution. It seems JPS have some kind of useless stuff............ So i was having that problem. By using this statement I could figure out my problem
<%@ page session="false" %>


Hey Nithin,

JSP's does not have useless stuff. Its just that your knowledge on jsp and servlet API is too weak If you look up the HttpServletRequest API you will notice there are two methods for getting the session:

HttpServletRequest.getSession() - This method returns a new session irrespective of whether one already exists...
HttpServletRequest.getSession(boolean) - If you pass false in here, It will always look for an existing session, if it doesnt find one, It will return null, if you pass true, it will check if there is an existing session, if it doesnt find one, It will create new one.

So in your code, when you wanted to check for the existing session, you should have done request.getSession(false), It would have given you an existing session.

Once you get hold of the session, you can set the attribute using HttpSession.setAttribute methods and use that attribute on every page where you need to display the userName.

I Hope I have been clear in making you understand your issue.

Thanks,
Yogendra.


Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 538

Yogendra Joshi wrote:HttpServletRequest.getSession() - This method returns a new session irrespective of whether one already exists...

No..
request.getSession(), will check for the existing one, if session exists it returns it.. if no current session only, it will create a new session..
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
Prasad Krishnegowda wrote:
Yogendra Joshi wrote:HttpServletRequest.getSession() - This method returns a new session irrespective of whether one already exists...

No..
request.getSession(), will check for the existing one, if session exists it returns it.. if no current session only, it will create a new session..


Hey Prasad,

Thanks for correcting me. I just looked the API and its exactly what you are saying.

Learning Day Today!

Yogendra
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 538

Yogendra Joshi wrote:
Prasad Krishnegowda wrote:

Hey Prasad,

Thanks for correcting me. I just looked the API and its exactly what you are saying.

Learning Day Today!

Yogendra


Welcome Brother..
nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 41
Yogendra Joshi wrote:
nithin chinni wrote:Sorry found the solution. It seems JPS have some kind of useless stuff............ So i was having that problem. By using this statement I could figure out my problem
<%@ page session="false" %>


Hey Nithin,

JSP's does not have useless stuff. Its just that your knowledge on jsp and servlet API is too weak If you look up the HttpServletRequest API you will notice there are two methods for getting the session:

HttpServletRequest.getSession() - This method returns a new session irrespective of whether one already exists...
HttpServletRequest.getSession(boolean) - If you pass false in here, It will always look for an existing session, if it doesnt find one, It will return null, if you pass true, it will check if there is an existing session, if it doesnt find one, It will create new one.

So in your code, when you wanted to check for the existing session, you should have done request.getSession(false), It would have given you an existing session.

Once you get hold of the session, you can set the attribute using HttpSession.setAttribute methods and use that attribute on every page where you need to display the userName.

I Hope I have been clear in making you understand your issue.

Thanks,
Yogendra.


Thanks for the reply. I know I am learning...... And I was in a foul mood with sessions. Anyways there is a problem here. Every time I reset my cookie start my server and then launch a new session I will get request.getSession(false) I will get some existing session. I dont want to set to some existing session. I want to check if there is new session for the first request and i am getting false when I check isNew(). So if I set in JSP that session="false" the session that I create when someone hit login page it will be a new session and thats just what I need.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Looks like you got it: a JSP will automatically create a new session if there is no session, by default. But you want to control the creation of sessions yourself, so that's a bad thing. So you fix your JSPs so they don't create new sessions. Right?
Souvik Dasgupta
Ranch Hand

Joined: Sep 24, 2009
Posts: 80


Hi Nitin,

Could you please set
inside the DemoServlet and retry?
The existing session will be used in this case, and new session will not be created.When you write , you create a new session and it does not have username set into it.


Thanks and Regards
Souvik


Thanks and Regards, Souvik
SCJP, SCWCD, OCEJWSD
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 538

nithin chinni wrote:So if I set in JSP that session="false" the session that I create when someone hit login page it will be a new session and thats just what I need

Setting session=false, just tells that the inbuilt jsp variable session is not available to this page. It doesn't mean that no session will be created..
nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 41
Souvik Dasgupta wrote:
Hi Nitin,

Could you please set
inside the DemoServlet and retry?
The existing session will be used in this case, and new session will not be created.When you write , you create a new session and it does not have username set into it.


Thanks and Regards
Souvik


I tried both of them. If its true then "when there is no session a new session will be automatically created" if its false" then it does not create a new session" and that is the only difference. But the thing is I am not getting a new session. When ever a use hits login page he has to get a new session. So that was what I am trying and it worked when I put session=false in the login.jsp page.
nithin chinni
Ranch Hand

Joined: Dec 02, 2010
Posts: 41
Prasad Krishnegowda wrote:
nithin chinni wrote:So if I set in JSP that session="false" the session that I create when someone hit login page it will be a new session and thats just what I need

Setting session=false, just tells that the inbuilt jsp variable session is not available to this page. It doesn't mean that no session will be created..


Oh, but what I needed is a new session when ever some one hits login.jsp page. I tried all the possibilities and finally I got the solution when I kept session=false.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Bad solution. You should not be trying to control when sessions are created and destroyed, You do not need to have a new session for a login. Simply control what's placed into the session rather than trying to control the session itself.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Bear Bibeault wrote:Bad solution. You should not be trying to control when sessions are created and destroyed, You do not need to have a new session for a login. Simply control what's placed into the session rather than trying to control the session itself.


hi sir bear,

how is the better/standard approach going to be when controlling what's placed in the session?.. is it checking for the attribute in sessions if it is empty or null? or compare what attribute was set?..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

You should check whether there object(s) you place in the session are there or not. If not there, you will receive a null.
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Bear Bibeault wrote:You should check whether there object(s) you place in the session are there or not. If not there, you will receive a null.


I see, what is the effect of using session invalidate rather than setting session attributes/objects to null?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Invalidate is a quick way to dump everything in the session.
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Bear Bibeault wrote:Invalidate is a quick way to dump everything in the session.


Okay. Thank you sir bear.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems with sessions