• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problems with sessions

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" %>
 
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 213
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 28364
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 113
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Invalidate is a quick way to dump everything in the session.
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Okay. Thank you sir bear.
 
That new kid is a freak. Show him this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic