my dog learned polymorphism*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes  forward() method of RequestDispatcher  vs  sendRedirect() on Response object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark " forward() method of RequestDispatcher  vs  sendRedirect() on Response object" Watch " forward() method of RequestDispatcher  vs  sendRedirect() on Response object" New topic
Author

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

shiv kedia
Greenhorn

Joined: Dec 11, 2005
Posts: 7
can anybody tell me the difference between
. forward() method of RequestDispatcher object
. sendRedirect() on Response object
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think that sendRedirect creates a new request, with a new header,
whereas forward keeps on using the same request.


[My Blog]
All roads lead to JavaRanch
Sub swamy
Ranch Hand

Joined: Oct 02, 2002
Posts: 121
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

Joined: Oct 09, 2003
Posts: 517
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.


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
When we use forward() or include() methods ,Can we pass custom(after change) request and response objects to forwarded or included resource?


<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Dec 06, 2005
Posts: 61
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

Joined: Nov 15, 2004
Posts: 1026
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward() method of RequestDispatcher vs sendRedirect() on Response object
 
Similar Threads
forward and sendRedirect
which one is fast
Help regarding a link to a page from servlet
Diiference between forward and redirect?
Illegatstateexception !!!