Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sharing session attributes between two servlets

 
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?
 
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.
 
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
 
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 ]
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic