• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mak Smash
Greenhorn
Posts: 17
C++ Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between RequestDispatcher() and sendRedirect()??
 
Manjula Weerasinghe
Greenhorn
Posts: 25
Java Oracle PHP
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good response from Mandula. This is a common question and is in the FAQ.
 
Mak Smash
Greenhorn
Posts: 17
C++ Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the key difference between using a < jsp:forward > and HttpServletResponse.sendRedirect()
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same answer.
 
Mak Smash
Greenhorn
Posts: 17
C++ Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this right to say < jsp:forward > runs on server side and HttpServletResponse.sendRedirect() runs on client side?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic