This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Send redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Send redirect" Watch "Send redirect" New topic
Author

Send redirect

ram kri
Greenhorn

Joined: Dec 08, 2005
Posts: 12
What is the difference between sendRedirect and forward in JSP?

[BPSouther: Fixed casing in post]
[ September 18, 2006: Message edited by: Ben Souther ]
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
ram kri. your caps lock is on. press switch it off.


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
source: http://www.sap-img.com/java/difference-between-response-sendredirect-and-request-forward.htm

sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response.

forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.

There are different situations where you want to use one or the other. For example, if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include(). If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect().
Nitin Nigam
Ranch Hand

Joined: Jul 03, 2006
Posts: 129
Hi All,
I think above question is very well answered. I have an additional question. If i want to use sendRedirect and want to pass some values to the new request then how do i go about it?
Suppose i am inside an application (a.jsp) and from here i want to jump to b.jsp(that jsp is in another application) then using sendRedirect will make a new request and all the session and request variables will be lost. How can i handle this situation?


Nothing is impossible; for those who doesnt have to do it themselves.
myjotting.blogspot.com
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
You can pass the sessionID and request parameters as GET parameters in the URL.


Ping & DNS - my free Android networking tools app
Nitin Nigam
Ranch Hand

Joined: Jul 03, 2006
Posts: 129
Hi Ulf,
Thanks for the responce. I was confused if there will be same session available when my request is going to a different application?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, sessions are per user, per application.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41180
    
  45
Ah, I missed that it was going to a different application. In that case, the session will be different, so there's no point in sending the session ID.
Nitin Nigam
Ranch Hand

Joined: Jul 03, 2006
Posts: 129
Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send redirect