【发布时间】:2012-09-23 12:34:40
【问题描述】:
这两天我一直在尝试找出问题所在。我在这里读到我应该在代码中添加一个返回,我做到了,我仍然得到 p>
java.lang.IllegalStateException: Cannot call sendRedirect()
after the response has been committed, Error.
我该如何解决这个问题?
每次我连接到数据库时都会发生这种情况。这是连接方法:
<%!
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);
}
}
%>
喜欢这个代码块:
String post = request.getParameter("send");
if(post != null )
{
connect();
statement.execute(add);
con.close();
response.sendRedirect("fourm.jsp");
return;
}
但在这段代码中完美地阻止了它的工作:
String back = request.getParameter("retrun");
if(back != null)
{
response.sendRedirect("fourm.jsp");
return;
}
【问题讨论】:
-
你能提供在两次调用“getParameter”之前运行的代码吗?
-
您还缺少其他东西,它会在您重定向()之后修改响应。
-
@Nambari 之前,确定吗?