Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

response sendRedirect() processing

    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 ?

like image 206
Sagar Rout Avatar asked Apr 28 '15 08:04

Sagar Rout


People also ask

How does Response sendRedirect work?

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.

What is the difference between response sendRedirect () and request forward ()?

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.

Can you call sendRedirect after response is committed?

you can't call sendRedirect(), after you have already used forward(). So, you get that exception.


2 Answers

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

like image 148
Dinesh Kannan Avatar answered Sep 30 '22 00:09

Dinesh Kannan


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.

like image 26
Laszlo Lugosi Avatar answered Sep 30 '22 01:09

Laszlo Lugosi