Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session Management in JSP

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • 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 ?



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
How would you ensure they are in the same web app context?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic