if(condition true){
request.sendRedirect("//Some URL");}
// Some other code
Now my question is when we redirect, the some other code will execute or not ? If execute when, before sendRedirect or after sendRedirect ?
sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.
Difference between forward() and sendRedirect() methodThe forward() method works at server side. The sendRedirect() method works at client side. It sends the same request and response objects to another servlet. It always sends a new request.
you can't call sendRedirect(), after you have already used forward(). So, you get that exception.
The sendRedirect
method does not halt the execution of your method.
You should either branch your code in such a way that the call to sendRedirect
is the last statement in your method, or explicitly call return;
after calling sendRedirect
.
See also http://www.coderanch.com/t/556146/Servlets/java/response-SendRedirect-session
It would be very confusing to do some stuff after redirect, so it should be the last statement of the flow. Commit, close your stuffs before redirect to help understanding your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With