File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward and sendRedirect methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "forward and sendRedirect methods" Watch "forward and sendRedirect methods" New topic
Author

forward and sendRedirect methods

Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
What is the difference between
RequestDispatcher.forward () and HttpServletResponse.sendRedirect()


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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).]


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
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

Joined: Jul 05, 2001
Posts: 2370

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

Joined: Aug 19, 2001
Posts: 341
Thanks people. I am putting another similar question. Please take time to answer that as well.
Chintan
herve attia
Ranch Hand

Joined: Sep 28, 2001
Posts: 138
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

Joined: Aug 19, 2001
Posts: 341
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

Joined: Dec 28, 2001
Posts: 21
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

Joined: Aug 19, 2001
Posts: 341
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

Joined: Dec 28, 2001
Posts: 21
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
 
subject: forward and sendRedirect methods