大家能否帮我解答下,我已经郁闷了一整天了,在网上查了很多解决的办法,但就是不行
是jsp+servlet报出异常
java.lang.IllegalStateException: Cannot forward after response has been committed
          myclass.StudentSvlt.sendBean(StudentSvlt.java:105)
  myclass.StudentSvlt.doGet(StudentSvlt.java:23)
  myclass.StudentSvlt.doPost(StudentSvlt.java:162)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:803) StudentSvlt 代码如下:package myclass;
import myclass.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class StudentSvlt extends HttpServlet{

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    
    String stu_id =req.getParameter("id");
    int success = 0;
    String action = action = req.getParameter("action");
    student stu = null;
    String message="";
   
    if ("new".equalsIgnoreCase(action)) {
      stu = doNew(req,res);     
      sendBean(req, res, stu, "/getStudent.jsp");
    }  
   
    if ("delete".equalsIgnoreCase(action)) {
     try{
       success = doDelete(stu_id);
           }
           catch(SQLException e){}
     if (success != 1) {
     doError(req, res, "StudentSvlt: Delete unsuccessful. Rows affected: " + success);
     } else {
     res.sendRedirect("getStudent.jsp");
return;
     }
    }
 } public student doNew(HttpServletRequest req,HttpServletResponse res ) throws ServletException,IOException{
      student stu= new student();                     
      String stu_id=req.getParameter("id");
      String name=new String(req.getParameter("name").getBytes("ISO-8859-1"));
      String password= req.getParameter("password");
      String dep=new String (req.getParameter("dep").getBytes("ISO-8859-1"));
      String sex = new String(req.getParameter("sex").getBytes("ISO-8859-1"));
      String jiguan = new String(req.getParameter("jiguan").getBytes("ISO-8859-1"));      if(isTrue(req,res,stu_id,name,password) && hasLogin(req,res,stu_id)){ 
           stu.setId(stu_id);
           stu.setName(name);
           stu.setPassword(password);
           stu.setDep(dep);
           stu.setSex(sex);
           stu.setJiguan(jiguan);
           stu.addStudent(); 
      }  
      return stu;                
 }
  ...........

 public void sendBean(HttpServletRequest req, HttpServletResponse res, student stu, String target)
                       throws ServletException, IOException {
    req.setAttribute("stu", stu);
    RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
    rd.forward(req, res);
    return;
 }
  
 public void doError(HttpServletRequest req, HttpServletResponse res, String str)
                      throws ServletException, IOException {
    req.setAttribute("problem", str);
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
    rd.forward(req, res);
    return;
 }
  
 public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String id)
  throws ServletException, IOException{
   boolean f=true;
   String message="对不起,该学生号已经被注册过了!";
   student stu= new student();
   f= stu.hasLogin(id);
   if(f==false){
      doError(req,res,message);
   }
   return f;
  }
  
  public boolean isTrue(HttpServletRequest req, HttpServletResponse res, 
                        String id,String name,String password)
                        throws ServletException, IOException {
   boolean f=true;                     
   String message ="";
   if(id==null || id.equals(""))  {
    f=false;
    message="错误,学生号不能为空!";
    doError(req,res,message); }
   
   if(name==null || name.equals(""))  {
    f=false;
    message="学生姓名不能为空,请重新填写!";
    doError(req,res,message); }
       
   if(password==null || password.equals(""))  {
    f=false;
    message="密码不能为空,请重新填写!";
    doError(req,res,message); }     return f;
  }
  
  public void doPost(HttpServletRequest req, HttpServletResponse res) 
              throws ServletException, IOException {
    doGet(req, res);
  }

}
jsp页面代码:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" import="myclass.*" errorPage="errorpage.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>增加学生</title>
</head>
<body bgcolor="#0099FF" text="#FFFFFF">
<p>
  <%
String admin_id = (String)session.getAttribute("id"); 
if(admin_id==null){response.sendRedirect("login.jsp");return;}
%>
</p>
<p align="center"><font color="#00FF00" size="+3" face="华文行楷">新增学生</font></p>
<form name="form1" method="post" action="StudentSvlt">
<input type="hidden" name="action" value="new">     。。  <p align="center"> 
    <input type="submit" name="Submit" value="确定">
  </p>
</form>
<a href="getStudent.jsp">&lt;&lt;Back </a> 
</body>
</html>---------------------------------------------------------------------------------------
我都已经改了n遍了,都不行,请大家帮我看看吧~~~ 谢谢

解决方案 »

  1.   

    你跟踪一下,你在servlet中自定义的方法中:doNew()方法里面能接收从jsp页面传过来的值吗
      

  2.   

    可以的,在doNew()方法中确实能拿到jsp的值,并且能够正常显示
    但是当我使用doError()方法时,就出现了那个错误
    我就很莫名啊~~~
      

  3.   

     Cannot   forward   after   response   has   been   committed 
    就是你在做forward之前已经提交到服务端了,就是在forward之前不能有提交语句!
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,   IOException {         
            String stu_id = req.getParameter("id"); 
            int success = 0; 
            String action = req.getParameter("action"); 
            student stu = null; 
            String message = ""; 
          
            if("new".equalsIgnoreCase(action)) { 
                stu = doNew(req,res);                  //doNew方法中一定有包含提交的语句
                sendBean(req,res,stu,"/getStudent.jsp");  //这句话之前不能有提交
            }     
          ...............................     
       }
    下面看你的doNew方法:
    public student doNew(HttpServletRequest req,HttpServletResponse res) throws   ServletException,IOException{ 
                student stu = new student();                                           
                String stu_id = req.getParameter("id"); 
                String name = new String(req.getParameter("name").getBytes("ISO-8859-1")); 
                String password = req.getParameter("password"); 
                String dep=new String(req.getParameter("dep").getBytes("ISO-8859-1")); 
                String sex = new String(req.getParameter("sex").getBytes("ISO-8859-1")); 
                String jiguan = new String(req.getParameter("jiguan").getBytes("ISO-8859-1"));             if(isTrue(req,res,stu_id,name,password)&&hasLogin(req,res,stu_id)){ //有问题 
                          stu.setId(stu_id); 
                          stu.setName(name); 
                          stu.setPassword(password); 
                          stu.setDep(dep); 
                          stu.setSex(sex); 
                          stu.setJiguan(jiguan); 
                          stu.addStudent();   
                }     
                return   stu;                                 
      } 
    你的isTrue和hasLogin方法中包含提交的语句,建议检验字符有效性最好再客户端进行,用javascript!!
      

  4.   

    程序在你  sendBean(req,   res,   stu,   "/getStudent.jsp"); 
    的时候已经进行了 
    RequestDispatcher   rd   =   getServletContext().getRequestDispatcher(target); rd.forward(req,   res); 
    也就是response向客户端返回了信息,之后的所有操作都无用,自然有错误
    所以
    sendBean方法中不应该返回的应该有判断的返回
      

  5.   

    请问for_cyan 
       什么叫做 有判断的返回 ?在 方法sendBean中:
    public   void   sendBean(HttpServletRequest   req,   HttpServletResponse   res,   student   stu,   String   target) throws   ServletException,   IOException   {
            req.setAttribute("stu",   stu);
            RequestDispatcher   rd   =   getServletContext().getRequestDispatcher(target);
            rd.forward(req,   res);
            return;
      } 
    应该在怎么样的情况下 return 呢?
      

  6.   

    请问 zxdu721
      你是否建议我在jsp中,用javascript来代替isTrue和hasLogin这两个方法?
    我试试看,谢谢你~~
      

  7.   

    请问 zxdu721
      你是否建议我在jsp中,用javascript来代替isTrue和hasLogin这两个方法?
    我试试看,谢谢你~