java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

后端 未结 8 1064
南笙
南笙 2020-11-21 11:06

This method throws

java.lang.IllegalStateException: Cannot forward after response has been committed

and I am unable to spot th

相关标签:
8条回答
  • 2020-11-21 11:34

    I removed

            super.service(req, res);
    

    Then it worked fine for me

    0 讨论(0)
  • 2020-11-21 11:35

    This is because your servlet is trying to access a request object which is no more exist.. A servlet's forward or include statement does not stop execution of method block. It continues to the end of method block or first return statement just like any other java method.

    The best way to resolve this problem just set the page (where you suppose to forward the request) dynamically according your logic. That is:

    protected void doPost(request , response){
    String returnPage="default.jsp";
    if(condition1){
     returnPage="page1.jsp";
    }
    if(condition2){
       returnPage="page2.jsp";
    }
    request.getRequestDispatcher(returnPage).forward(request,response); //at last line
    }
    

    and do the forward only once at last line...

    you can also fix this problem using return statement after each forward() or put each forward() in if...else block

    0 讨论(0)
  • 2020-11-21 11:35

    You should add return statement while you are forwarding or redirecting the flow.

    Example:

    if forwardind,

        request.getRequestDispatcher("/abs.jsp").forward(request, response);
        return;
    

    if redirecting,

        response.sendRedirect(roundTripURI);
        return;
    
    0 讨论(0)
  • 2020-11-21 11:46

    After return forward method you can simply do this:

    return null;
    

    It will break the current scope.

    0 讨论(0)
  • 2020-11-21 11:48

    even adding a return statement brings up this exception, for which only solution is this code:

    if(!response.isCommitted())
    // Place another redirection
    
    0 讨论(0)
  • 2020-11-21 11:53

    A common misunderstanding among starters is that they think that the call of a forward(), sendRedirect(), or sendError() would magically exit and "jump" out of the method block, hereby ignoring the remnant of the code. For example:

    protected void doXxx() {
        if (someCondition) {
            sendRedirect();
        }
        forward(); // This is STILL invoked when someCondition is true!
    }
    

    This is thus actually not true. They do certainly not behave differently than any other Java methods (expect of System#exit() of course). When the someCondition in above example is true and you're thus calling forward() after sendRedirect() or sendError() on the same request/response, then the chance is big that you will get the exception:

    java.lang.IllegalStateException: Cannot forward after response has been committed

    If the if statement calls a forward() and you're afterwards calling sendRedirect() or sendError(), then below exception will be thrown:

    java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

    To fix this, you need either to add a return; statement afterwards

    protected void doXxx() {
        if (someCondition) {
            sendRedirect();
            return;
        }
        forward();
    }
    

    ... or to introduce an else block.

    protected void doXxx() {
        if (someCondition) {
            sendRedirect();
        } else {
            forward();
        }
    }
    

    To naildown the root cause in your code, just search for any line which calls a forward(), sendRedirect() or sendError() without exiting the method block or skipping the remnant of the code. This can be inside the same servlet before the particular code line, but also in any servlet or filter which was been called before the particular servlet.

    In case of sendError(), if your sole purpose is to set the response status, use setStatus() instead.


    Another probable cause is that the servlet writes to the response while a forward() will be called, or has been called in the very same method.

    protected void doXxx() {
        out.write("some string");
        // ... 
        forward(); // Fail!
    }
    

    The response buffer size defaults in most server to 2KB, so if you write more than 2KB to it, then it will be committed and forward() will fail the same way:

    java.lang.IllegalStateException: Cannot forward after response has been committed

    Solution is obvious, just don't write to the response in the servlet. That's the responsibility of the JSP. You just set a request attribute like so request.setAttribute("data", "some string") and then print it in JSP like so ${data}. See also our Servlets wiki page to learn how to use Servlets the right way.


    Another probable cause is that the servlet writes a file download to the response after which e.g. a forward() is called.

    protected void doXxx() {
        out.write(bytes);
        // ... 
        forward(); // Fail!
    }
    

    This is technically not possible. You need to remove the forward() call. The enduser will stay on the currently opened page. If you actually intend to change the page after a file download, then you need to move the file download logic to page load of the target page.


    Yet another probable cause is that the forward(), sendRedirect() or sendError() methods are invoked via Java code embedded in a JSP file in form of old fashioned way <% scriptlets %>, a practice which was officially discouraged since 2001. For example:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            ... 
        </head>
        <body>
            ...
    
            <% sendRedirect(); %>
            
            ...
        </body>
    </html>
    

    The problem here is that JSP internally immediately writes template text (i.e. HTML code) via out.write("<!DOCTYPE html> ... etc ...") as soon as it's encountered. This is thus essentially the same problem as explained in previous section.

    Solution is obvious, just don't write Java code in a JSP file. That's the responsibility of a normal Java class such as a Servlet or a Filter. See also our Servlets wiki page to learn how to use Servlets the right way.


    See also:

    • What exactly does "Response already committed" mean? How to handle exceptions then?

    Unrelated to your concrete problem, your JDBC code is leaking resources. Fix that as well. For hints, see also How often should Connection, Statement and ResultSet be closed in JDBC?

    0 讨论(0)
提交回复
热议问题