• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
shiv kedia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me the difference between
. forward() method of RequestDispatcher object
. sendRedirect() on Response object
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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.
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • 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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • 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
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Yogesh Hingmire
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic