当前位置: 代码迷 >> 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,该如何解决

热度:135   发布时间:2016-04-22 00:30:43.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
  相关解决方案