Cannot call sendRedirect() after the response has been committed

Cannot call sendredirect() after the response has been committed exception

For the HTTP response, if it has been submitted (redirection, request forwarding, release in the filter), you can’t perform any operation on the response, such as modifying it or submitting it again.

For the submitted response, the method will not end (different from return), it will continue to execute, so if the code that you continue to execute after submitting will still run, the submit response will also cause this error.

For example:

    1. the following code will report an error because it released and submitted the response, and now it operates the response, because the filter release does not return the method, and the method is still executed downward </ OL> instead

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        boolean boo = "Your judgment conditions";
        if (boo) {
            log.info("enter chain");
            chain.doFilter(request, response);
            //return;
        }
        //This is where the error will be reported because the response was submitted in the above release and now the resposne is being operated
        httpResponse.sendRedirect(TOLOGINURL);

2. The following code will report an error because it is submitted again after redirection, because the redirection will continue


httpResponse.sendRedirect(TOLOGINURL);
//This will report an error because it was committed (redirected) and then committed again
httpResponse.sendRedirect(TOLOGINURL);

The solution is to return after submitting</ Code>, or the check logic will not enter the submit again.

Read More: