File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward and sendRedirect methods

 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between
RequestDispatcher.forward () and HttpServletResponse.sendRedirect()
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Chintan, in coding its tough to find the difference. But theoritically, HTTPServletResponse object's sendRiderect() will drive the request temporarilly to another resource to be processed or responsed. But RequestDispathcer's forward() will drive the request not to be processed by one servlet, rather by another servlet or JSP or else. sendRedirect() converts the relative URL to absolute URL be4 sending the response to client which forward() does not. But still waiting to see a difference in execution time or performance....

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
[This message has been edited by Ashik uzzaman (edited December 30, 2001).]
 
Harpreet Hira
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chintan,
Ashik has explained it well, but I would like to explain a bit more. Both sendRedirect and forward are used to pass the request to another HTML, JSP or servlet. But sendRedirect is less efficient if we need to pass the request to a resource in the same context because in sendRedirect the request is first sent to the client which requests for the resource mentioned in the argument of sendRedirect method.
Thus sendRedirect request has to move to and fro from webserver to client to webserver to reach the resource whereas forward acts directly within webserver.

------------------
Harpreet S Hira
Sun Certified Java2 Programmer
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very clear explanation Harppet...thnx.

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks people. I am putting another similar question. Please take time to answer that as well.
Chintan
 
herve attia
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be a little bit more clear :
1)
-SendRedirect needs the client to reconnect to the new URL.
-forward method of RequestDispatcher is handled completely on the server
2)
-SendRedirect do not preserve all the request data.
-forward method of RequestDispatche does !!
3)
-SendRedirect results in a different final URL
-forward maintains the URL of the original servlet.
Regards,
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understand why forward is preferred over sendRedirect but then why would we have sebdRedirect. I cannot think of a situation where we would need sendRedirect over forward.
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need it if you have to redirect a broswer to another website. Say from yahoo.com to lycos.com. You cannot even forward a request to another servlet/jsp of a different webapplication.
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aah I see.
But.. why did you say "you cannot forward request to a servlet or JSP of another web app" ?
I think in ServletContext interface there is a method
ServletContext getContext(String path)
This method could return a ServletContext of another webapp. Once you have servlet context you could forward request to any JSP or Servlet of that webapp.
However, I fully agree with yahoo to lycos part.
 
Vaishali Joshi
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chintan Rajyaguru:
I think in ServletContext interface there is a method
ServletContext getContext(String path)

Yeah, I didn't consider that.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic