sendRedirect() method in HttpServletResponse object takes one parameter String representing the URL, and automatically set the Http 302 status code with appropriate header.
a call to forward() method may be used only if no content has been previously sent to a client. No further data can be sent to the client after the forward has completed. the forward() method delegates a request and response to the resource of the RequestDispatcher object.
With Forward, request & response would be passed to the destination URL, which should be relative (means that the destination URL should be within a servlet context. RequestDispatcher rd=getServletContext().getNamedDispatcher("HelloServlet"); rd.forward(request,response); ). Also, after executing forward method, the control will return back to the same method from where the forward method was called. All the opposite to the above points apply to sendRedirect.
The forward will redirect in the application server itself. It does not come to the client, whereas Response.sendredirect () will come to the client and go back ...i.e. URL appending will happen. You can't call sendRedirect() after calling forward(). Once the forward completes, the response is closed. You can't call any method which sets headers or writes output to the server.
Hope this info is helpful.
Joined: Aug 15, 2004
watch your statements. first you said Also, after executing forward method, the control will return back to the same method from where the forward method was called.
its not like that anyways, then you are saying
You can't call sendRedirect() after calling forward(). Once the forward completes, the response is closed. You can't call any method which sets headers or writes output to the server.
even after calling sendRedirect() you can't call forward.
actually sendRedirect() and forward() are not similar. if we say what is the difference b/w include() and forward() then it makes sense. and i couldn't get you. wht do you mean by "doesn't to the client" and "will come to the client". read the docs. as Jules said there is no substitute. [ September 07, 2004: Message edited by: adeel ansari ]