Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

forward and sendRedirect methods  RSS feed

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between
RequestDispatcher.forward () and HttpServletResponse.sendRedirect()
 
Ranch Hand
Posts: 2373
Java MySQL Database Spring
  • 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).]
 
Ranch Hand
Posts: 72
  • 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
Java MySQL Database Spring
  • 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
  • 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
 
Ranch Hand
Posts: 138
  • 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
  • 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.
 
Greenhorn
Posts: 21
  • 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
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!