java. Lang. IllegalStateException: sendredirect() cannot be called after submitting the response

Two days I tried to find out what was wrong I read here that I should add a return to the code, and then I did it, and I still got it

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

How can I solve this problem?

This happens every time I connect to a database This is the connection method:

<%!

public  void connect()
{
        try {
            Class.forName("com.MysqL.jdbc.Driver");
            String dbURL = "jdbc:MysqL://localhost:3306/moti";
            String user = "root";
            String password = "j3o4h5n6y7";
            con =  DriverManager.getConnection(dbURL,user,password);  
            statement = con.createStatement();
        }
        catch(Exception ex) {
            throw new Error(ex);
        }  
}
%>

As in this code block:

String post = request.getParameter("send");
            if(post != null )
            {
                    connect();
                    statement.execute(add);
                    con.close();
                    response.sendRedirect("fourm.jsp");
                    return;

            }

But in this code, it works perfectly:

String back = request.getParameter("retrun");

    if(back != null)
    {

        response.sendRedirect("fourm.jsp");
        return;
    }

Solution

At a high level, your specific problem is caused by your wrong use of JSP files instead of servlet classes as front-end controllers

At a low level, your specific problem is that JSP plays the role of view technology during generating HTML code and sending it to HTTP response The default response buffer size is 2KB Once the code reaches that line, each HTML and other template text in the JSP is immediately written to the response Therefore, when the response buffer size limit is reached for the first time, all HTTP response headers and HTML code written so far will be sent to the client (WebBrowser) In other words, the response has been submitted This is a road of no return It is impossible to get the sent bytes from the client

Redirection basically sets the location header on the HTTP response In order to set it correctly, it is obviously impossible to submit a response If they have been sent and retrieved by the client, the new response header cannot be set at all

At a low level, you can solve your specific problem by moving all front-end controllers and business logic to the top of the JSP file so that it can be executed long before the first HTML code is sent This eliminates the risk of submitting responses before the front-end controller and business logic are completed

<%@page pageEncoding="UTF-8" %>
<%
    // Write business code here.
%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Some</title>
    </head>
    <body>
        ... (no controller/business logic here! just pure presentation)
    </body>
</html>

However, this is a bad practice Instead, move all front-end controllers and business logic to servlet Then you can see the correct method from the advanced level Java code does not belong to JSP file, but belongs to Java class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>