File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Session is not preserverd whn sendRedirect is used ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session is not preserverd whn sendRedirect is used ???" Watch "Session is not preserverd whn sendRedirect is used ???" New topic
Author

Session is not preserverd whn sendRedirect is used ???

Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
Hi,

Guys i found the following quote in one of the forums, and am not convinced with this statement, let me know if this is true, plzz

When you forward the request with requestDispatcher.forward the target servlet/JSP share the same session. But with sendRedirect the session information is not preserved. ( target is not in scope of session)


-Tripter
Aadi Narayana Reddy
Greenhorn

Joined: Oct 17, 2005
Posts: 13
hi,


No that statement is wrong as my knowledge goes.
i have worked with the servlets and used the sendredirect mechanism, and the session info is preserved there, im able to get the session object from there also


regards,
aadi


G.Adi Narayana Reddy,
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

As far as I am aware it is wrong. The only condition I can think where it may be correct is the situation where the sendRedirect is the response to the first request, and this response is also the one trying to set the session cookie. I'm not convinced but it may be possible.

eg.


In this case the instruction either may not be sent or accepted and hence the session would not be created , but but I'm not sure, and I'm not convinced.

Like I said, sounds wrong.
Rakesh Gadre
Ranch Hand

Joined: Feb 22, 2003
Posts: 47
It looks like a typo error. It seems that the para refers to the requst scope rather than session.
Srivatsa Katta
Ranch Hand

Joined: Aug 13, 2005
Posts: 55
Hi,

Thanks for sharing your views guys!!

-Tripter
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Hello

Check the thread again. It is correct.

http://www.coderanch.com/t/170618/java-Web-Component-SCWCD/certification/sendRedirect-Vs-requestdispatcher-forward


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
OK, I see where the confusion is coming from. The quote is not correct, or in the very least worded very badly. But I think I understand what the author was trying to get at.

If you use sendRedirect to redirect the user to a different context, i.e. a different application, of course the session information is not preserved. It would be meaningless because sessions are tied to a specific context. (for example, my session information from javaranch would have no meaning if I decided to go to yahoo)

However, using sendRedirect within the same context works perfectly. The author of the previous quote mistakenly implies that sendRedirect is only used when changing contexts, however, this is not the case. There are times when it is advisable to redirect within the same context. For example, if you need to send the user to a new page, but wanted to maintain the previous page's URL inthe browser cache.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Another case when the redirect is to switch to different protocol http to https.


Groovy
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026


if you need to send the user to a new page, but wanted to maintain the previous page's URL in the browser cache.




Don't you think for this you need to go for RequestDispatch rather than sendRedirect?
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Don't you think for this you need to go for RequestDispatch rather than sendRedirect?

My mistake (early morning here ) I should have said "if you need to send the user to a new page, and wanted to maintain both the previous page's URL and the new page's URL in the browser cache."
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Another case when the redirect is to switch to different protocol http to https.


What would happen in above situation - sendRedirect() to same context but changing to ssl. I dont know about SSL much but its seems that it has some built-in facility to identify clients. And in that a case, a newly created SSL session should be considered as some new session. Am I right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session is not preserverd whn sendRedirect is used ???