日期:2014-05-18  浏览次数:20727 次

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");
}
}

/**
 *