• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Difference between forward() and sendRedirect()  RSS feed

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain me clearly what is the difference between
Difference between forward() and sendRedirect().
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forward action takes place within the server without the knowledge of client whereas sendredirect is just explicit redirection of url with client aware of that
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, basically both method calls redirect u to new resource/page/servlet.
the difference between the two is that sendRedirect always sends a header back to the client/browser. this header then contains the resource(page/servlet) which u wanted to be redirected. the browser uses this header to make another fresh request. thus sendRedirect has a overhead as to the extra remort trip being incurred. its like any other Http request being generated by ur browser. the advantage is that u can point to any resource(whether on the same domain or some other domain).

for eg if sendRedirect was called at www.abc.com then it can also be used to redirect a call to a resource on www.xyz.com. where as in case of forward() call, the above is not true. resources from the server, where the fwd. call was made, can only be requested for. but the major diff between the two is that forward just routes the request to the new resources which u specify in ur forward call. that means this route is made by the servlet engine at the server level only. no headers r sent to the browser which makes this very efficient. also the request and response objects remain the same both from where the forward call was made and the resource which was called.

the main difference is in response.sendRedirect() you need to give absolute path of the url and also request and response objects will be passed automatically to the new page. But in the case of forward method, you need to explicitly send the request and response objects to the new page explicitly.

1) If you use a RequestDispatcher, the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute(). With a sendRedirect(), it is a new request from the client, and the only way to pass data is through the session or with web parameters (url?name=value).

2) A sendRedirect() also updates the browser history. Suppose you have JSP-1 which has a form that targets Servlet-2, which then redirects to JSP-3. With a redirect, the user's address bar will read "http://[host]/JSP-3". If the user clicks the Reload/Refresh button, only JSP-3 will be re-executed, not Servlet-2.
[ August 25, 2004: Message edited by: Sadatcharam Rajendran ]
And then we all jump out and yell "surprise! we got you this tiny ad!"
global solutions you can do in your home or backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!