运行的时候总是出现这个,请高手指点帮忙解决一下.java.lang.IllegalStateException: Cannot forward after response has been committed
ClassSvlt.sendBean(ClassSvlt.java:123)
ClassSvlt.doGet(ClassSvlt.java:21)
ClassSvlt.doPost(ClassSvlt.java:161)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import mm.course;public class CourseSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String cour_id =req.getParameter("id");
int success = 0;
String action = action = req.getParameter("action");
course cour = null;
String message="";
if ("new".equalsIgnoreCase(action)) {
cour = doNew(req,res);
sendBean(req, res, cour, "/getcourse.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try{
cour = doUpdate(req,res, cour_id);
sendBean(req,res,cour,"/getcourse.jsp");
}
catch(SQLException e){}
}
if ("delete".equalsIgnoreCase(action)) {
try{
success = doDelete(cour_id);
}
catch(SQLException e){}
if (success != 1) {
doError(req, res, "CourseSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/test/getcourse.jsp");
}}
}
public course doNew(HttpServletRequest req,HttpServletResponse res )
throws ServletException,IOException{
course cour= new course();
String cour_id=req.getParameter("id");
int ;
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
try{
= Integer.parseInt(req.getParameter(""));
}catch(NumberFormatException e){=0;}
String dep=new String (req.getParameter("dep").getBytes("ISO8859_1"));
String prepare =req.getParameter("prepare");
if(isTrue(req,res,cour_id,name) && hasLogin(req,res,cour_id) && isCompare(prepare,dep,req,res)){
cour.setId(cour_id);
cour.setName(name);
cour.setDep(dep);
cour.setPrepare(prepare);
cour.setMark();
cour.addCourse();
}
return cour;
}
public boolean isCompare(String prepare,String dep,
HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
boolean f=true;
String tempDep=null;
String message=null;
course cour= new course();
if( !prepare.equalsIgnoreCase("0") ){
tempDep=cour.getPrepareDep(prepare) ;
if(tempDep.equals("public"))
return true;
if(dep.equalsIgnoreCase(tempDep))
f=true;
else {
f=false;
message="错误,课程所在系与预修课所在系不一致!";
doError(req,res,message);
}}
return f;
}
public course doUpdate(HttpServletRequest req,HttpServletResponse res , String id)
throws ServletException,IOException,SQLException {
course cour = new course();
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
int = Integer.parseInt(req.getParameter(""));
String dep = req.getParameter("dep");
String prepare = req.getParameter("prepare");
if(isTrue(req,res,id,name) && isCompare(prepare,dep,req,res)){ cour.setName(name);
cour.setMark();
cour.setDep(dep);
cour.setPrepare(prepare); cour.updateCourse(id);}
return cour;
}
public int doDelete(String id) throws SQLException {
int num=0;
course cour=new course();
num=cour.deleteCourse(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
course cour, String target)
throws ServletException, IOException {
req.setAttribute("cour", cour);
RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
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);
}
public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String id)
throws ServletException, IOException{
boolean f=true;
String message="对不起,该课程号已经被注册过了!";
course cour= new course();
f= cour.hasLogin(id);
if(f==false){
doError(req,res,message);
}
return f;
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id,String name)
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); }
return f;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { doGet(req, res); }
}
代码是下面这两个:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import mm.classp;public class ClassSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String class_id =req.getParameter("id");
int success = 0;
String action = action = req.getParameter("action");
classp cla = null;
String message="";
if ("new".equalsIgnoreCase(action)) {
cla = doNew(req,res);
sendBean(req, res, cla,"/getClass.jsp");
}
if ("update".equalsIgnoreCase(action)) {
try{
cla = doUpdate(req,res, class_id);
sendBean(req,res,cla,"/getClass.jsp");
}
catch(SQLException e){}
}
if ("delete".equalsIgnoreCase(action)) {
try{
success = doDelete(class_id);
}
catch(SQLException e){}
if (success != 1) {
doError(req, res, "ClassSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/test/getClass.jsp");
}}
}
public classp doNew(HttpServletRequest req,HttpServletResponse res )
throws ServletException,IOException{
classp cla= new classp();
String class_id=req.getParameter("id");
String tea_id=req.getParameter("tea_id");
String cour_id=req.getParameter("cour_id");
String room_id=req.getParameter("room_id");
String cour_time=req.getParameter("cour_time");
if(isTrue(req,res,class_id) && hasMoreclass(tea_id,cour_time,req,res) ){
cla.setId(class_id);
cla.setTea_id(tea_id);
cla.setCour_id(cour_id);
cla.setRoom_id(room_id);
cla.setCour_time(cour_time);
cla.addClass();
}
return cla;
}
public boolean hasMoreclass(String tea_id,String cour_time,HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
boolean f=true;
String temp="";
String message="";
classp cla=new classp();
temp=cla.hasMoreclass(tea_id,cour_time);
if (temp=="no")
f=true;
else {
f=false;
message="对不起,该教师("+tea_id+")在"+cour_time+"时间已经安排有课"+temp+"" ;
//doError(req,res,message);
}
return f;
}
public boolean hasChange(String tea_id,String cour_time,
HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
boolean f=false;
String tea_id0=req.getParameter("tea_id0");
String cour_time0=req.getParameter("cour_time0");
if(!tea_id.equals(tea_id0) || !cour_time.equals(cour_time0) )
f=true;
return f;
}
public classp doUpdate(HttpServletRequest req,HttpServletResponse res , String id)
throws ServletException,IOException,SQLException {
classp cla = new classp();
String tea_id=req.getParameter("tea_id");
String cour_id=req.getParameter("cour_id");
String room_id=req.getParameter("room_id");
String cour_time=req.getParameter("cour_time");
if(hasChange(tea_id,cour_time,req,res)){
if(hasMoreclass(tea_id,cour_time,req,res)){
cla.updateClass(id,tea_id,cour_id,room_id,cour_time);
}}else{
cla.updateClass(id,cour_id,room_id);
}
return cla;
}
public int doDelete(String id) throws SQLException {
int num=0;
classp cla=new classp();
num=cla.deleteClass(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
classp cla, String target)
throws ServletException, IOException {
req.setAttribute("cla", cla);
RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
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);
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id)
throws ServletException, IOException {
classp cla = new classp();
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="错误,班级号不能为空!";
doError(req,res,message); }
if(cla.hasLogin(id)){
f=false;
message="对不起,班级("+id+")已经注册了!";
doError(req,res,message);
}
return f;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { doGet(req, res); }
}
import java.sql.*;
public class classp { private String id;
private String cour_id;
private String tea_id;
private String room_id;
private String cour_time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCour_id() {
return cour_id;
}
public void setCour_id(String cour_id) {
this.cour_id = cour_id;
}
public String getTea_id() {
return tea_id;
}
public void setTea_id(String tea_id) {
this.tea_id = tea_id;
}
public String getRoom_id() {
return room_id;
}
public void setRoom_id(String room_id) {
this.room_id = room_id;
}
public String getCour_time() {
return cour_time;
}
public void setCour_time(String time) {
this.cour_time = time;
}
public ResultSet getTeachers(){
String sql="select id,name from teacher ";
sqlBean db = new sqlBean();
ResultSet rs = db.executeQuery(sql);
return rs;
}
public ResultSet getCourses(){
String sql="select id,name from course ";
sqlBean db = new sqlBean();
ResultSet rs = db.executeQuery(sql);
return rs;
}
public boolean hasLogin(String id){
boolean f=false;
String sql="select id from classes where id ='"+id+"'";
sqlBean db =new sqlBean();
try{
ResultSet rs=db.executeQuery(sql);
if(rs.next()){ f=true;}
else{ f=false;}
}catch(Exception e){ e.getMessage();}
return f;
}
public String hasMoreclass(String tea_id,String cour_time){ //检查教师是否同一时间上两门课程
String temp="";
String sql="select id from classes "+
"where tea_id='"+tea_id+"' and cour_time='"+cour_time+"' ";
sqlBean db =new sqlBean();
try{
ResultSet rs=db.executeQuery(sql);
if(rs.next()){ temp=rs.getString("id");}
else{ temp="no";}
}catch(Exception e){ System.out.print(e.getMessage());}
return temp;
}
public void addClass(){
String sql="insert into classes(id,tea_id,cour_id,room_id,cour_time) "+
"values('"+id+"','"+tea_id+"','"+cour_id+"','"+room_id+"','"+cour_time+"') ";
try{
sqlBean db =new sqlBean();
db.executeInsert(sql);
} catch(Exception e){System.out.print(e.toString());}
}
public void updateClass(String id,String tea_id,String cour_id,
String room_id,String cour_time){
String sql="update classes "+
" set tea_id='"+tea_id+"',cour_id='"+cour_id+"',"+
"room_id='"+room_id+"',cour_time='"+cour_time+"' "+
" where id='"+id+"' ";
sqlBean db= new sqlBean();
db.executeInsert(sql);
}
public void updateClass(String id,String cour_id,
String room_id){
String sql="update classes "+
" set cour_id='"+cour_id+"',"+
"room_id='"+room_id+"' "+
" where id='"+id+"' ";
sqlBean db= new sqlBean();
db.executeInsert(sql);
}
public int deleteClass(String id){
int num=0;
String sql="delete from classes where id ='"+id+"' ";
sqlBean db= new sqlBean();
num=db.executeDelete(sql);
return num;
}
public ResultSet getClasses(){
String sql="select classes.id,tea_id,cour_id,room_id,cour_time, "+
"course.name as cour_name,teacher.name as tea_name "+
"from classes ,course,teacher "+
"where classes.cour_id=course.id "+
"and classes.tea_id=teacher.id ";
sqlBean db= new sqlBean();
ResultSet rs = db.executeQuery(sql);
return rs;
}
}