wood burning stoves 2.0*
The moose likes Servlets and the fly likes Diff btw response.sendRedirect, jsp:forward and RequestDispatch .forward methods? 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 » Servlets
Bookmark "Diff btw response.sendRedirect, jsp:forward and RequestDispatch .forward methods?" Watch "Diff btw response.sendRedirect, jsp:forward and RequestDispatch .forward methods?" New topic
Author

Diff btw response.sendRedirect, jsp:forward and RequestDispatch .forward methods?

David Tan
Greenhorn

Joined: Sep 30, 2002
Posts: 9
What is the difference between the following:
1) response.sendRedirect()
2) jsp:forward
3) RequestDispatcher .forward
4) RequestDispatcher .include
Any examples to illustrate the differences and how each of them is applied?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
David, welcome to JavaRanch
This is a very broad question, and I would highly recommend for you to use the search facility. These questions are asked many times in the ranch, with excellent replies.
click here to use the search. If, after reading thorougly previous posts you still have doubts, let us know


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Amol Takate
Greenhorn

Joined: Sep 20, 2003
Posts: 15
1) response.sendRedirect() :
It actually modifies response header and tells the client the browser that the requested page is relocated and header also contains the new location of the page. Then browser again sends the request at this new location. Thus your request gets redirected. You can redirect the request any where in the web using sendRedirect.
2) jsp:forward :
Same as RequestDispatcher.forward() , but jsp syntax

3) RequestDispatcher .forward
No response is send to the browser, the web container internally redirects the request to the other page. Remember , you can redirect only to a page within current servlet context.
Also there are two flavours of RequestDispatcher , one : you get from ServletContext .The pathname must begin with a "/" and is interpreted as relative to the current context root.
Second : you get from ServletRequest. The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root

4) RequestDispatcher.include
Redirects the page (as said above) but temporarily and it returns again back to the original page.
For more details refer API docs which comes with j2ee SDK.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Diff btw response.sendRedirect, jsp:forward and RequestDispatch .forward methods?
 
Similar Threads
How to catch exception from Servlet in error.jsp
RequestDispatcher forward doubt
sendRedirect issue
How do I redirect the browser post to a URL from within my servlet?
Difference between <jsp:forward> and RequestDispatcher