wood burning stoves*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp:foward vs response.sendRedirect" Watch "jsp:foward vs response.sendRedirect" New topic
Author

jsp:foward vs response.sendRedirect

Jo Liang
Greenhorn

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.

HttpServletResponse.sendRecirect:
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.
regards
sanj
Geeta Gune
Greenhorn

Joined: Mar 19, 2002
Posts: 17
hi
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:
Ex:Checking.jsp

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

Joined: Apr 21, 2002
Posts: 48
Hello!
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.
 
 
subject: jsp:foward vs response.sendRedirect
 
Similar Threads
error with jsp:foward
Servlet calling another servlet...
diff between redirect and requestdispatcher
servlet
a question about jsp:foward