• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference b/w forward and sendRedirect

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want the difference between forward and sendRedirect Method
any one plz help....
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok, there's really no substitute for reading the API docs.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic