java.lang.IllegalStateException:提交响应后无法调用sendRedirect() [英] java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

查看:1606
本文介绍了java.lang.IllegalStateException:提交响应后无法调用sendRedirect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两天来,我一直试图找出问题出在哪里.我在这里读到我应该在代码中添加一个返回值,并且做到了,但我仍然得到

For two days I have tried find out what went wrong. I read here that i should add a return into the code, and I did it, and i still get

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

我该如何解决这个问题?

How can I solve this problem?

每次我连接到数据库时都会发生这种情况.这是连接方法:

It happens every time I connect to the database. This is the connect 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);
        }  
}
%>

就像在此代码块中一样:

like 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 block its work perfectly :

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

    if(back != null)
    {

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

推荐答案

从高层次看,您的具体问题是由于您错误地使用了JSP文件而不是

From high level seen, your concrete problem is caused because you're incorrectly using a JSP file instead of a Servlet class as a front controller.

从低端看,您的具体问题是由于在生成HTML代码并将其发送到HTTP响应期间,JSP作为一种视图技术而起的作用.响应缓冲区大小默认为2KB.一旦代码到达该行,JSP中的每个HTML和其他模板文本都将立即写入响应中.因此,当第一次达到响应缓冲区大小限制时,所有HTTP响应标头和到目前为止编写的HTML代码将被发送到客户端(Web浏览器).换句话说,响应已提交.这是无可挽回的一点.根本不可能从客户端取回已发送的字节.

From low level seen, your concrete problem is caused because JSP plays as being a view technology a role during generating and sending HTML code to the HTTP response. The response buffer size defaults to 2KB. Every HTML and other template text in the JSP is immeditately written to the response once the code reaches that line. So when the response buffer size limit is reached for the first time, then all HTTP response headers and the so far written HTML code will be sent to the client (the webbrowser). In other words, the response is committed. This is a point of no return. It's simply not possible to take the already-sent bytes back from the client.

重定向基本上在HTTP响应上设置了Location标头.为了能够正确设置此值,显然必须尚未提交响应.如果客户机已经发送和检索了所有响应头,则根本不可能设置新的响应头.

A redirect basically sets a Location header on the HTTP response. To be able to properly set this, the response must obviously not be committed yet. It's simply not possible to set a new response header if they are all already been sent and retrieved by the client.

从低端看,您可以通过将所有前端控制器和业务逻辑移到JSP文件的最顶端来解决您的具体问题,以便在发送第一个HTML代码之前就可以执行它.这样,您就消除了在前端控制器和业务逻辑完成之前提交响应的风险.

From low level seen, you can solve your concrete problem by moving all the front controller and business logic to the far top of the JSP file so that it's executed long before the first HTML code is ever been sent. This way you eliminate the risk that the response is committed before your front controller and business logic is finished.

<%@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>

但是,这是一个错误练习.而是将所有前端控制器和业务逻辑移到 Servlet 中.那么您的方法是从高层次看正确的. Java代码不属于JSP文件,而是Java类.

However, this is a bad practice. Rather move all that front controller and business logic into a Servlet. Then your approach is from high level seen correct. Java code doesn't belong in JSP files, but in Java classes.

这篇关于java.lang.IllegalStateException:提交响应后无法调用sendRedirect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆