• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between Response.sendRedirect and RequestDispatcher.forward

 
Ranch Hand
Posts: 182
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?



 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic