• 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

forward() method of RequestDispatcher vs sendRedirect() on Response object

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody tell me the difference between
. forward() method of RequestDispatcher object
. sendRedirect() on Response object
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that sendRedirect creates a new request, with a new header,
whereas forward keeps on using the same request.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) forward() is a server-side redirect. url on the browser doesn't change.
2) sendRedirect() happens on the client-side i.e server sends a redirect url to a client status of http 301 and the url on the browser changes to the redirected value.
 
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forward() method of RequestDispatcher is a server side activity. So the request and its associated session are available to the forwarded resource.This method is normally used for sending a request and response object to resources (servlets or JSP's)which are in the same ServletContext.

sendRedirect() method of a response object sends the url(parameter of sendRedirect() method) to the browser and the browser sends a new request to that url. sendRedirect() used to forward requests to a resource which is outside of the current web application. It is like ,opening a new browser and type your url.


correct me if i am wrong.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Request parameters and request attributes are NOT preserved with sendRedirct.

The client browser makes a automatic request on the URL specified in the redirect(301) response header. You can see the change in the request URL in the browser
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we use forward() or include() methods ,Can we pass custom(after change) request and response objects to forwarded or included resource?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can. You can add Query strings at the end of the resource name to which the request is forwarded.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more addition, i guess that with RequestDispatcher we cannot redirect to a resource that is in a different context, while with redirect we can.

Can u guys confirm the same
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RequestDispatcher in ServletContext can be used to dispatch the request to a different context.

Take a look at this method too in ServletContext.


getContext(String uripath)

 
reply
    Bookmark Topic Watch Topic
  • New Topic