Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference b/w forward and sendRedirect

 
Ashok Srinivasan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the difference between forward and sendRedirect Method
any one plz help....
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashok, there's really no substitute for reading the API docs.
 
Jyothi Lature
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic