aspose file tools*
The moose likes Servlets and the fly likes What is difference between RequestDispatcher() and sendRedirect()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What is difference between RequestDispatcher() and sendRedirect()?" Watch "What is difference between RequestDispatcher() and sendRedirect()?" New topic
Author

What is difference between RequestDispatcher() and sendRedirect()?

Mak Smash
Greenhorn

Joined: May 15, 2011
Posts: 17

What is difference between RequestDispatcher() and sendRedirect()??
Manjula Weerasinghe
Greenhorn

Joined: Mar 15, 2010
Posts: 25

Hi Mak,

RequestDispatcher forward method pass the control of the request to another servlet or jsp without telling anything about the request dispatch to the client browser. Therefore client browser don't know whether the returned resource is from an another servlet/jsp or not.

sendRedirect method stop further processing of the request and send http status code "301" and URL of the location to be redirected to the client browser in the response header. Server does not have control of this request after sending the redirect related HTTP header to the client browser. Client browser sees http status 301 and then it know it should send a new request to the url in "Location" http header which is set by server. and Client browser sends a new request to the new URL and it will be processed by the server as another normal request.

Therefore request dispatch happens completely in the server side.
But sendRedirect is handle through the client browser.

Thanks & Regards,
Manjula


OCPJP 6, OCE JEE 6 JSP and Servlet Developer
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Good response from Mandula. This is a common question and is in the FAQ.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mak Smash
Greenhorn

Joined: May 15, 2011
Posts: 17

What is the key difference between using a < jsp:forward > and HttpServletResponse.sendRedirect()
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Same answer.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mak Smash
Greenhorn

Joined: May 15, 2011
Posts: 17

Is this right to say < jsp:forward > runs on server side and HttpServletResponse.sendRedirect() runs on client side?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Not really. They both run on the server. But, a sendRedirect causes a status code to be sent to the browser that instructs it to make a new request to the redirect URL.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is difference between RequestDispatcher() and sendRedirect()?
 
Similar Threads
SendRedirect Vs RequestDispatcher
what is the difference between forward and sendredirect
what is requestDispatcher
what isthe main difference between sendredirect and Requestdispatcher when we preffered each other
What is the difference between requestdispatcher and sendredirect?