Sagar Rout
Sagar Rout

Reputation: 694

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 ?

Upvotes: 12

Views: 6280

Answers (2)

Dinesh Kannan
Dinesh Kannan

Reputation: 1255

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

Upvotes: 12

Laszlo Lugosi
Laszlo Lugosi

Reputation: 3829

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.

Upvotes: 2

Related Questions