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.
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
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,
Joined: Aug 19, 2001
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.
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.
Joined: Aug 19, 2001
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.
Joined: Dec 28, 2001
Originally posted by Chintan Rajyaguru: I think in ServletContext interface there is a method ServletContext getContext(String path)