File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes jsp:foward vs response.sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp:foward vs response.sendRedirect" Watch "jsp:foward vs response.sendRedirect" New topic

jsp:foward vs response.sendRedirect

Jo Liang

Joined: Dec 10, 2001
Posts: 11
Hi there,
What is the difference between jsp:foward and HttpServletResponse.sendRedirect??
Here is some API regarding to each technique.
<jsp:forward />:
This method is used to re-direct, or "forward" the current ServletRequest and ServletResponse to another active component in the application.

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs.
But does that really make difference when coding the application?
Thank you.
- Jo
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
the main diffrence is that the forward one does the redirecting at the server side while the sendRedirect one redirects to a new URL at the client's side ( the browser).
the sendRedirect one also loses the request and response data you may have put in (like forms attributes etc) while the forward one doesnt.
sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
Hi Jo,
One big advantage of sendRedirect() is that you can send your request to another Context.
Geeta Gune

Joined: Mar 19, 2002
Posts: 17
the main difference between jsp forward and redirect is that the code execution stops after u use the jsp forward while in redirect the code is executed till the end.
For ex. try the foll code in a jsp file with both redirect and forward:

here ..only "i=5" shall be printed out to the means that the code execution has stopped after forward.
but in redirect the code execution does not it will print both the statements.
so according to me its better to use forward than redirect.
Hope this helps.
Amir Kamran
Ranch Hand

Joined: Apr 21, 2002
Posts: 48
jsp:forward is used to trasfer control to some other resource a static or a dynamic page. It is totally on server even the url bar of the client is not change. and also by passing the reference of request and response objects u can use them in the forwarding page.
In case of sendRedirect(), first a error code is send to the client and the connection of http is break. Then a new request is send to the server. This causes a new request and response objects. U can not send attributes with request.
Another difference is that in sendRedirect() the connection is made more than once b/w server and client the process is slow as compered to forward which is totally on server.
I agree. Here's the link:
subject: jsp:foward vs response.sendRedirect
It's not a secret anymore!