File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Session Management in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Session Management in JSP" Watch "Session Management in JSP" New topic
Author

Session Management in JSP

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 175
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 175
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

A JSP will automatically have a session -- you do not need to, and should not try to, manage it yourself.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 175
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

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

Joined: Feb 21, 2014
Posts: 92
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

Joined: Jan 10, 2002
Posts: 60992
    
  65

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

Joined: Jan 10, 2002
Posts: 60992
    
  65

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

Joined: Feb 21, 2014
Posts: 92
How would you ensure they are in the same web app context?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

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

Joined: Feb 21, 2014
Posts: 92
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Management in JSP