aspose file tools*
The moose likes Certification Results and the fly likes Difference between forward() and sendRedirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Certification Results
Bookmark "Difference between forward() and sendRedirect()" Watch "Difference between forward() and sendRedirect()" New topic

Difference between forward() and sendRedirect()

Sanj Sharma
Ranch Hand

Joined: Feb 14, 2004
Posts: 32
Can anyone please explain me clearly what is the difference between
Difference between forward() and sendRedirect().
Pandurangan Rakesh

Joined: Jun 06, 2004
Posts: 24
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
Sadatcharam Rajendran

Joined: Aug 25, 2004
Posts: 1
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 then it can also be used to redirect a call to a resource on 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 ]
I agree. Here's the link:
subject: Difference between forward() and sendRedirect()
Similar Threads
Request dispatcher
difference between forward and include
jsp forward and sendRedirect("")...?
what is the difference between forward and sendredirect