• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between Response.sendRedirect and RequestDispatcher.forward  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my code I tried to use both methods for redirecting a user to another page. I read the differences on google.
I noticed that I need to specify the app name in the redirect path used in response.sendRedirect, but not in
RequestDispatcher,forward. Please tell me why this happens ?



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A redirect sends a response back to the browser to request the new page. Since the URL is nitrated from the browser, it needs the path to the web app. Otherwise how would it know where to send the new request.

A forward happens on the server and only works within the same web app, so no web app info is needed. The server already knows what it is.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forwarding is an internal transfer to another resource within the web application, whereas redirecting can transfer to any URL whatsoever, anything in the whole web. So when redirecting, you can't assume that the target is within your web application (although it often will be). That's why you have to specify your application name if you're redirecting to an address within your application.
 
Aijaz Mallick
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Response.sendRedirect will totally send a New Request with all the attributes set in the Request Object Lost <i.e> All the attributes you set in the Request Object will be lost.

RequestDispatcher.forward - Is an Internal transfer with all your Attributes present in the request Object at the Destination page.

Its just like Send a totaly New Email - Which is blank And Request.forward is like Forwarding an Existing Mail(Request) with all its previous contents
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!