• Post Reply Bookmark Topic Watch Topic
  • New Topic

Session values still exist after using response.sendRedirect()

 
Ravisekhar Kovuru
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
we know that response.sendRedirect(newURL) will create new request and response objects before redirecting to the newURL. See the below code:

When I access the session attribute in the newURL page, I am still getting its value as "Value1" using request.getSession().getAttribute("sessionAttr");
Eventhough the request object is new, howcome request.getSession() returns the same old session object?? Any explanations...??
Thanks in advance....

-Ravi
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65520
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the whole purpose of the session. It will stay around until it either times out or you explicitly invalidate it.
 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the send Redirect method you are just redirecting your request to another request handler and hence only your request objects are redirected to the corresponding request handler. As the current request handler and forwarded request handler are both in the same session, the session objects will persists till the session is available.

Regards,
M.S.Raman
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravisekhar,

Session is something like a life time, a scope, in which the value will
be alive. So a session can have more than one request-response cycles.

All the more, we set a value to the session attribute to make it
available all through the life of the session.

We can end a session by calling session.invalidate() method. That's when
the seiion gets destroyed and the value won't be available any more.

Hope this helps about the session and the request-response.

Cheers,
Swamy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!