File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Difference b/w forward and sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference b/w forward and sendRedirect" Watch "Difference b/w forward and sendRedirect" New topic
Author

Difference b/w forward and sendRedirect

Ashok Srinivasan
Greenhorn

Joined: Jun 24, 2004
Posts: 15
I want the difference between forward and sendRedirect Method
any one plz help....
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
sendRedirect() method in HttpServletResponse object takes one parameter String representing the URL, and automatically set the Http 302 status code with appropriate header.

a call to forward() method may be used only if no content has been previously sent to a client. No further data can be sent to the client after the forward has completed. the forward() method delegates a request and response to the resource of the RequestDispatcher object.

hope it would help you.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Ashok, there's really no substitute for reading the API docs.
Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hello Ashok,

With Forward, request & response would be passed to the destination URL, which should be relative (means that the destination URL should be within a servlet context.
RequestDispatcher rd=getServletContext().getNamedDispatcher("HelloServlet");
rd.forward(request,response); ).
Also, after executing forward method, the control will return back to the same method from where the forward method was called. All the opposite to the above points apply to sendRedirect.

(OR)

The forward will redirect in the application server itself. It does not come to the client, whereas Response.sendredirect () will come to the client and go back ...i.e. URL appending will happen.
You can't call sendRedirect() after calling forward(). Once the forward completes, the response is closed. You can't call any method which sets headers or writes output to the server.

Hope this info is helpful.


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
watch your statements. first you said

Also, after executing forward method, the control will return back to the same method from where the forward method was called.

its not like that anyways, then you are saying


You can't call sendRedirect() after calling forward(). Once the forward completes, the response is closed. You can't call any method which sets headers or writes output to the server.

even after calling sendRedirect() you can't call forward.

actually sendRedirect() and forward() are not similar. if we say what is the difference b/w include() and forward() then it makes sense. and i couldn't get you. wht do you mean by "doesn't to the client" and "will come to the client". read the docs. as Jules said there is no substitute.
[ September 07, 2004: Message edited by: adeel ansari ]
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
In simple words,

sednRedirect tells the browser to look for the resource elsewhere. What this means is, it is not transparent to the browser.

Where as, forward is processed on the server side. Servlet container looks for the resource that is being requested thru forward and forwards the request and response object to it.


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
To add to this.....


sendRedirect makes another round trip from server to client thereby
loosing all attributes set in request scope....whereas while using forward
u will retain the attributes set in request scope......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference b/w forward and sendRedirect