• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session Management in JSP

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I have some doubt in session management in JSP.
In JSP we can get session object by defining in "session = "true " " in the page directive.

Taking some cases we used to do in servlet :

1.) request.getSession(true)
In servlet it says to allocate a new session object to the request if there is no session object corresponding this request. How would we do this in JSP ?

2.) request.getSession(false)
In servlet it says to use the already existing session object corresponding to this request, in case not present, do not allocate a new session object to this request. How would we do this in JSP ?



 
Sheriff
Posts: 67746
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 don't. Unless you turn it off, the JSP will already have a session. You don't try to manage it yourself.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You don't. Unless you turn it off, the JSP will already have a session. You don't try to manage it yourself.



Thanks Bear...but sorry to say i didn't get it, Please elaborate it a bit. Thanks!!

 
Bear Bibeault
Sheriff
Posts: 67746
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
A JSP will automatically have a session -- you do not need to, and should not try to, manage it yourself.
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:A JSP will automatically have a session -- you do not need to, and should not try to, manage it yourself.



I have got your point but if a JSP automatically have a session object then why have they given provision to enable or disable session from page directive ?
 
Bear Bibeault
Sheriff
Posts: 67746
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
As I said in my first post, you can tell the JSP engine not to make the session available, but if you don't, a session will be available. It's as simple as that.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what he is saying in a way I guess. I have been trying to learn the same thing. Bear I know exactly what you mean. For me personally, when I create a session from a servlet and render the response to the JSP the JSP was sent the session object. That starts the session.
For me, when I click a link like 'contact us' (not involving a servlet) - (just linking me to another JSP page and go back) the session is forgotten.

When on a JSP, and going from one page to next without any servlet mapping, how would you keep that Session?
ALSO if there was mapping to a servlet, would I need to do (request.getSession();) in every single servlet?

I am not sure but to the original poster in this topic, that may help clarify your issue. I am possibly having the same one.
 
Bear Bibeault
Sheriff
Posts: 67746
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
The point is that you don't need to do anything. Everyone seems to over think managing the session and usually just end up gumming up the works. Let the container manage the session and no one gets hurt.
 
Bear Bibeault
Sheriff
Posts: 67746
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
P.S. If your session "forgotten" between resources in the same web app (context), it's likely the resources aren't actually in the same context. Otherwise, the session would be maintained.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you ensure they are in the same web app context?
 
Bear Bibeault
Sheriff
Posts: 67746
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

Jeremy McNally wrote:How would you ensure they are in the same web app context?



By making sure that the context is deployed correctly. Sometimes people just copy stuff into the webapps folder rather haphazardly, or stick stuff into another web app (like ROOT), and all heck breaks loose.

Or, (self-fulfilling prophecy) by trying to micro-manage the session, they end up boofing things up.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do sort of understand. I will be checking this out a little further. If I still don't get it after my research and trials I will post my own topic on it with example issues. Thanks again Bear.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic