Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing session attributes between two servlets

 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I understand that on saying response.sendRedirect(), a new request would be created (effectively, the previously existing request would be lost). But, will the existing session be still available thru request.getSession(false)?
The problem I am facing is:
1. I have two servlets running in my system, one for each application.
2. I want the values stored in the session to be shared between the two servlets (like When servlet1 redirects a particular request to servlet2).
3. Apart from composing a url with all the details needed by servlet2, I need certain other data to be taken from the session.
Is it possible?
 
Ivan Tamayo
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, if you want to call a servlet from another, use a RequestDispatcher.
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session attributes are only shared by servlets within the same web application (i.e. war). Beyond that, they are not shared. Further, you of course cannot forward to a servlet in another app--you must redirect.
 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks for the info. Following this, if I return back to servlet 1 from servlet 2, will the values that I had stored in the session (before redirecting to servlet2) still be intact?
The scenrio is like this:
1. I store some values in the session , before redirecting to servlet2 from servlet1.
2. when servlet2 redirects back to servlet1, i want servlet1 to pick up the previously stored values and continue.
As it might be obvious, I am a beginner in this and not sure of my approach.
Rhea
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values in the session will remain until one of the following happens:
1. They are explicity removed using session.removeAttribute().
2. The sessions expires due to client inactivity.
A session basically represents a conversation between a client (i.e the browser) and the server. Providing the user doesn't leave his desk for a long lunch break or sit looking at the same page for too long then the session will be retained on the server.
HTH
[ June 04, 2003: Message edited by: Andy Bowes ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic