• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Tan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amol Takate
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic