Exact Difference between jsp:forward and RequestDispatcher.sendRedirect()

I want to know the exact Difference between jsp:forward and RequestDispatcher.sendRedirect(), give me some 3 differences
[127 byte] By [kiran_mpina] at [2007-10-3 2:21:29]
# 1
i only thought of redirect makes new request to the server and forward retains the request and goes to another page
jgalacambraa at 2007-7-14 19:20:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> Exact Difference between jsp:forward and RequestDispatcher.sendRedirect

Correction ..

Its response.sendRedirect and not RequestDispatcher.sendRedirect

Ok ..I'll tell u 6 differences ...

1) In forward mechanism the request is forwarded from server to server .i.e the request objects are posted internally within the server and hence client [browser] is not involved.

In sendRedirect communication is sent to the browser to redirect its request and thus makes a round trip with the browser.

2) Forward is much faster in performance compared to redirect due to no round trip with browser and because no headers are sent to the browser.

3) In forward the URI does not change. However in redirect, the URI is changed.

4) Forward will not work in a different servlet context. Ex: U cant forward requests from Yahoo.com to java.sun.com

Redirect works in both same/ as well as different contexts.

5) Forward retains the request, but with redirect all request parameters are lost.

6) U need to provide the complete URI during redirect Ex: "http://...."

but with forward only relative path is sufficent.

did I miss any other difference ;-)

By the way this used to be a common question I used to ask around 3 yrs back to newcomers[1-2 yrs exp] while conducting interviews in my company.

Regards

-Rohit Kumar

RohitKumara at 2007-7-14 19:20:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> i only thought of redirect makes new request to the

> server and forward retains the request and goes to

> another page

Almost. A forward does indeed retain the request because a forward is performed on the server: control is simply passed to another resource while a redirect is performed by the browser. A redirect will change the URL and will also end up in the page history, a forward does not.

gimbal2a at 2007-7-14 19:20:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
> U need to provide the complete URI during redirect Ex: " http://...."but with forward only relative path is sufficent.No you don't :s A relative path in a redirect will just translate to the current browser path.
gimbal2a at 2007-7-14 19:20:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

My mistake on point# 6 ..tx

But one needs to be careful when writing the url.

Ex ..I have "/code" as my web application context path :-

A.jsp :

<jsp:forward page="/jsp/backend/login.jsp" />

<%

System.out.println("Hello world .. forward test ;-)");

%>

B.jsp :-

<%

response.sendRedirect("/code/jsp/backend/login.jsp");

System.out.println("Hello world ..redirect test ;-)");

%>

So in A.jsp if we add the context root then this forward will fail ...

<jsp:forward page="/code/jsp/backend/login.jsp" />

Similarly in B.jsp if remove the application context path then this will fail ..

response.sendRedirect("/jsp/backend/login.jsp");

Now a question to U :

QUES: Will "Hello world .. forward test ;-)" be printed ?

What about "Hello world ..redirect test ;-)" ?

Its a simple question ..I'll reply the answer if no one posts by tomorrow morning.

RohitKumara at 2007-7-14 19:20:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
So no one posted the answer yet ..;-)Anyways ..here is the answer ..The mesage "Hello world .. forward test ;-)" during<jsp:forward ..will not get printed. However it does get printed during response.sendRedirect(...).Cheers-Rohit>
RohitKumara at 2007-7-14 19:20:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...