This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Servlets and the fly likes Sharing session attributes between two servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sharing session attributes between two servlets" Watch "Sharing session attributes between two servlets" New topic

Sharing session attributes between two servlets

Rhea Karnam

Joined: May 06, 2003
Posts: 21
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

Joined: Aug 13, 2001
Posts: 49
In general, if you want to call a servlet from another, use a RequestDispatcher.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
Rhea Karnam

Joined: May 06, 2003
Posts: 21
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.
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
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.
[ June 04, 2003: Message edited by: Andy Bowes ]

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
I agree. Here's the link:
subject: Sharing session attributes between two servlets
It's not a secret anymore!