Why the code executes after the redirection in java?
We were working on a redirection in Java. We found that even after redirect Java was executing following codes. Lets consider this basic example:
res.sendRedirect(""); System.out.println("This was printing");
We should explicitly call return
after redirect. After the redirect it is not possible to write to response. After using sendRedirect method, the response should be considered to be committed. The response should not be written further.
If we don’t want the following code to be executed we can simply return like below:
return res.sendRedirect(""); System.out.println("This was printing");