当前位置: 代码迷 >> J2EE >> HTTP Status 500 - Cannot call sendRedirect() after the response has been committ解决思路
  详细解决方案

HTTP Status 500 - Cannot call sendRedirect() after the response has been committ解决思路

热度:648   发布时间:2016-04-19 22:36:56.0
HTTP Status 500 - Cannot call sendRedirect() after the response has been committ
package package1;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletConfig;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterProcessAction
 */
public class RegisterProcessAction extends HttpServlet {
private static final long serialVersionUID = 1L;
    private Connection cn = null;
    private String drivername = null;
    private String url = null;
    private String user = null;
    private String password = null;
    public void init(ServletConfig config) throws ServletException
    {
     super.init(config);
     drivername = config.getInitParameter("drivername");
     url = config.getInitParameter("url");
     try{
     Class.forName(drivername);
     cn = DriverManager.getConnection(url,user,password);
     }catch(Exception e){
     System.out.println("取得数据库连接错误:"+e.getMessage());
     }
    }
    

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userid = request.getParameter("userid");
String password = request.getParameter("password");
String repassword = request.getParameter("repassword");
String name = request.getParameter("name");
if(userid==null||userid.trim().length()==0){
response.sendRedirect("register.jsp");
}
if(password==null||password.trim().length()==0){
response.sendRedirect("register.jsp");
}
if(repassword==null||repassword.trim().length()==0){
response.sendRedirect("register.jsp");
}
if(!password.equals(repassword)){
response.sendRedirect("register.jsp");
}
if(name!=null&&name.trim().length()>0){
name = new String(name.getBytes("UTF-8"));
}
String sql = "insert into register(userid,name,password)values(?,?,?)";
try{
PreparedStatement ps = cn.prepareStatement(sql);
ps.setString(1, userid);
ps.setString(2, name);
ps.setString(3, password);
ps.executeUpdate();
ps.close();
response.sendRedirect("success.jsp");
}catch(Exception e){
System.out.println("错误:"+e.getMessage());
response.sendRedirect("register.jsp");
}
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
public void destroy(){
super.destroy();
try{
cn.close();
}catch(Exception e){
System.out.println("关闭数据库错误:"+e.getMessage());
}
}

}

运行之后老是出现HTTP Status 500 - Cannot call sendRedirect() after the response has been committed

--------------------------------------------------------------------------------

type Exception report

message Cannot call sendRedirect() after the response has been committed

description The server encountered an internal error (Cannot call sendRedirect() after the response has been committed) that prevented it from fulfilling this request.

exception 

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
package1.RegisterProcessAction.doGet(RegisterProcessAction.java:45)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.29 logs.

求高手解答!


------解决方案--------------------
500是服务器错误,一般是连接不上数据库或配置哪里写错了,你先注释掉
package1.RegisterProcessAction.doGet(RegisterProcessAction.java:45)这里的代码,然后一步一步的调错试试。
------解决方案--------------------
在每个response.sendRedirect() 调用后面加上return
------解决方案--------------------

    ActionForward forward = new ActionForward("XXX.jsp");
    forward.setRedirect(true);
  相关解决方案