Cannot forward after response has been committed问题解决及分析

通过TOMCAT把系统启动,可以正常登陆门户,登陆进去选择子系统的时候点击登陆的时候,可是去又回到了登陆界面,如此反复就是不能够进入子系统,查看后台报的错误:

Cannot forward after response has been committed

中文意思就是已经有提交了,不能够再次转向了,然后根据JSP标签中设置的错误页面又回到了登陆页面,在网上搜索了一下找到这样一篇文章,以下为转贴,地址为:http://heisetoufa.ggblog.com/299425.html

 

我也就报着试一下的心态将原来的代码:

改成了如下:

这段代码是在一个方法里,并且在调用这个方法的的方法里面,就在该方法的后也加了一个return;

就没有再次报如标题的错误了。

 

本文出自:冯立彬的博客



Cannot forward after response has been committed

06-21

package com.csms.action.servlet;rnrnimport java.io.IOException;rnimport java.io.PrintWriter;rnimport java.sql.SQLException;rnimport java.util.ArrayList;rnrnimport javax.servlet.ServletException;rnimport javax.servlet.http.HttpServlet;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport com.csms.dao.InterImpl.Oder_inFactory;rnimport com.csms.dao.inter.Oder_inInter;rnimport com.csms.dao.model.Oder_in;rnrnpublic class Oder_inServlet extends HttpServlet rnrn /**rn * Constructor of the object.rn */rn public Oder_inServlet() rn super();rn rnrn /**rn * Destruction of the servlet. rn */rn public void destroy() rn super.destroy(); // Just puts "destroy" string in logrn // Put your code herern rnrn /**rn * The doDelete method of the servlet. rn *rn * This method is called when a HTTP delete request is received.rn * rn * @param request the request send by the client to the serverrn * @param response the response send by the server to the clientrn * @throws ServletException if an error occurredrn * @throws IOException if an error occurredrn */rn public void doDelete(HttpServletRequest request,rn HttpServletResponse response) throws ServletException, IOException rnrn // Put your code herern rnrn /**rn * The doGet method of the servlet. rn *rn * This method is called when a form has its tag value method equals to get.rn * rn * @param request the request send by the client to the serverrn * @param response the response send by the server to the clientrn * @throws ServletException if an error occurredrn * @throws IOException if an error occurredrn */rn public void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn doPost(request,response);rn rnrn /**rn * The doPost method of the servlet. rn *rn * This method is called when a form has its tag value method equals to post.rn * rn * @param request the request send by the client to the serverrn * @param response the response send by the server to the clientrn * @throws ServletException if an error occurredrn * @throws IOException if an error occurredrn */rn public void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rnString opt=request.getParameter("opt");rn rn rn rn if(opt.equals("update"))rn rn try rn doupdate(request,response);rn catch (SQLException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn rn rn rn /**rn * rn */rn public void doupdate(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException, SQLException rn// String Oder_no=request.getParameter("oder_nos");rn// System.out.println("oder_no:"+Oder_no);rn// Oder_inFactory oder_infac=new Oder_inFactory();rn// Oder_inInter oder_ininter=oder_infac.getOder_inImpl();rn// Oder_in oder_in =new Oder_in();rn// oder_in=oder_ininter.queryOder_in(Oder_no);rn// rn//// request.setAttribute("oder_in", oder_in);rn// response.sendRedirect(request.getContextPath()+"stock/order2002.jsp");rn request.getRequestDispatcher("/stock/order2002.jsp").forward(request, response);rn return ;rn rn rn /**rn * Initialization of the servlet. rn *rn * @throws ServletException if an error occursrn */rn public void init() throws ServletException rn // Put your code herern rndebug 跟踪request.getRequestDispatcher("/stock/order2002.jsp").forward(request, response);时报rn2012-6-21 18:23:07 org.apache.catalina.core.StandardWrapperValve invokern严重: Servlet.service() for servlet Oder_inServlet threw exceptionrnjava.lang.IllegalStateException: Cannot forward after response has been committedrn at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)rn at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)rn at com.csms.action.servlet.Oder_inServlet.doupdate(Oder_inServlet.java:144)rn at com.csms.action.servlet.Oder_inServlet.doPost(Oder_inServlet.java:92)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)rn at java.lang.Thread.run(Thread.java:619)rn

求助解决错误:Cannot forward after response has been committed!

08-01

我的方法代码如下,方法可以正常执行,可总会报这个错误,请问如何解决,我判断是我最后一句return的问题,可我用return null也试了还是不行!!!rnpublic String doTest1()rn List list=loadByIds();rn //if(list==null||list.size()>1) throw new RuntimeException("请选择某一卷来打印案卷封面!");rn Iterator ite=list.iterator();rn TemplateForm tf=(TemplateForm) ite.next();rn String qz=tf.getValue("全宗号");rn String bm=QzAdminAction.getQzNameformQzh(qz);rn String ml=tf.getValue("目录号");rn String ajh=tf.getValue("案卷号");rn String bgqx=tf.getValue("保管期限");rn String qssj=tf.getValue("文件起始时间");rn String jssj=tf.getValue("文件结束时间");rn String js=tf.getValue("件数");rn String ys=tf.getValue("页数");rn String lb=tf.getValue("类别");rn String ajmc=tf.getValue("案卷名称");rn int bytesRead = 0;rn int c;rn String filename="C://ajfm.xls";rn tryrn //Excel获得文件rn InputStream stream=new FileInputStream("D://ajfm.xls");rn FileOutputStream os=new FileOutputStream(filename);rn while((c=stream.read())!=-1) rn os.write(c);rn os.flush(); rn os.close();rn Workbook wb=Workbook.getWorkbook(new File(filename));rn //打开一个文件的副本,并且指定数据写回到原文件rn WritableWorkbook book=Workbook.createWorkbook(new File(filename),wb);rn WritableSheet sheet=book.getSheet(0);rn //WritableCell wc = sheet.getWritableCell(0, 0);rn WritableFont wf = new WritableFont(WritableFont.createFont("华文中宋"),48); rn WritableCellFormat wcfF = new WritableCellFormat(wf);rn WritableFont wf1 = new WritableFont(WritableFont.createFont("宋体"),12); rn WritableCellFormat wcfF1 = new WritableCellFormat(wf1);rn WritableFont wf2 = new WritableFont(WritableFont.createFont("宋体"),20); rn WritableCellFormat wcfF2 = new WritableCellFormat(wf2);rn WritableFont wf3 = new WritableFont(WritableFont.createFont("华文中宋"),28); rn WritableCellFormat wcfF3 = new WritableCellFormat(wf3);rn wcfF.setWrap(true);rn wcfF.setAlignment(jxl.format.Alignment.CENTRE);rn wcfF.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);rn wcfF1.setWrap(true);rn wcfF1.setAlignment(jxl.format.Alignment.CENTRE);rn wcfF1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);rn wcfF2.setWrap(true);rn wcfF2.setAlignment(jxl.format.Alignment.CENTRE);rn wcfF2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);rn wcfF3.setWrap(true);rn wcfF3.setAlignment(jxl.format.Alignment.CENTRE);rn wcfF3.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);rn sheet.addCell(new Label(0,0,bm,wcfF));rn sheet.addCell(new Label(0,1,lb,wcfF3));rn sheet.addCell(new Label(1,3,ajmc,wcfF2));rn sheet.addCell(new Label(0,4,"自"+qssj+"至"+jssj,wcfF2));rn sheet.addCell(new Label(3,4,bgqx,wcfF2));rn sheet.addCell(new Label(0,5,"本卷共"+js+"件"+ys+"页",wcfF2));rn sheet.addCell(new Label(2,8,qz,wcfF1));rn sheet.addCell(new Label(3,8,ml,wcfF1));rn sheet.addCell(new Label(4,8,ajh,wcfF1));rn book.write(); rn book.close();rn FileInputStream istream = new FileInputStream(filename);rn byte[] buffer =new byte[8192];rn response.setContentType("application/vnd.ms-excel");rn response.setHeader("content-disposition", "inline; filename="+filename);rn ServletOutputStream ouputStream = response.getOutputStream();rn while ((bytesRead = istream.read(buffer, 0, 8192)) != -1) rn ouputStream.write(buffer, 0, bytesRead);rn rn response.setContentLength(bytesRead);rn ouputStream.flush(); rn ouputStream.close();rn istream.close();rn catch(Exception e) rn rn e.printStackTrace(); rn rn File mydir = new File(filename);rn mydir.delete();rn return BaseAction.CLOSEME;rn

Cannot forward after response has been committed(着急解决)

11-30

package com.course;rnimport java.io.*;rnimport java.sql.ResultSet;rnimport java.sql.*;rnimport javax.servlet.*;rnimport javax.servlet.http.*;rnrnpublic class StudentLoginSvlt extends HttpServletrn //响应get请求rnpublic void doGet(HttpServletRequest req, HttpServletResponse res)rn throws ServletException, IOException rn //接收提交的跟选课相关的各种信息 rn String stu_id =req.getParameter("id");rn String cour_id=req.getParameter("cour_id");rn String class_id=req.getParameter("class_id");rn String prepare=req.getParameter("prepare");rn String pw1=null;rn String pw2=null;rn String e_mail=null;rn String tel=null;rn //获得请求中的参数rn String action = req.getParameter("action");rnrn ResultSet rs=null; rn //如果请求中是更新参数rn if ("update".equalsIgnoreCase(action)) rnrn stu_id =req.getParameter("id");rn pw1=req.getParameter("password1");rn pw2=req.getParameter("password2");rn if(pw1.equals("") || pw2.equals("") || pw1==null || pw2==null)rn doError(req,res,"密码不能为空!");rn e_mail=req.getParameter("e_mail");rn tel=req.getParameter("tel");rn doUpdate(req,res,pw1,pw2,e_mail,tel,stu_id);rn res.sendRedirect("student.jsp");rn rn rn //如果请求中是更改成绩参数 rn if ("checkmark".equalsIgnoreCase(action)) rn rs=getScore(stu_id) ;rn sendResultSet(req,res,rs,"/checkmark.jsp");rn rn //如果参数中是注册课程参数rn if("enrol".equalsIgnoreCase(action))rn rn doEnrol(req,res,stu_id,cour_id,class_id,prepare); rn res.sendRedirect("DisplayCourse.jsp");rnrn rn rn //注册课程课方法rn public void doEnrol(HttpServletRequest req, HttpServletResponse res,rn String stu_id,String cour_id,String class_id,String prepare)rn rn throws ServletException, IOException rn //System.out.println("test doEnrol"); rn int num=0; rn //声明JavaBean对象rn checkEnrol check=new checkEnrol();rn //System.out.println(check);rn //如果预修课学分为0,则注册 rn if(prepare.equals("0") ) rn num= check.enrol(class_id,stu_id); rn else rn //判断预修课是否符合要求 rn if( check.hasPassPrepare(prepare)) rn num= check.enrol(class_id,stu_id);rn else doError(req,res,"清先完成预修课"); rn rn rn if(num==0)rn doError(req,res,"注册课程失败!!");rn rn rn //修改学生信息rn public void doUpdate(HttpServletRequest req, HttpServletResponse res,rn String pw1,String pw2,String e_mail,String tel,String id)rn throws ServletException, IOExceptionrn int num=0;rn if(!pw1.equals(pw2))rn doError(req,res,"密码不一致,请重输!");rn checkEnrol check=new checkEnrol(); rn sqlBean db = new sqlBean();rn num= check.updatestu(pw1,id,e_mail,tel);rn if(num==0) doError(req,res,"更新失败");rn rn rn rn //获得课程成绩 rn public ResultSet getScore(String stu_id)rn String sql="select enrol.score , course.name ,course.mark "+rn "from enrol ,course ,classes "+rn "where stu_id='"+stu_id+"' "+rn "and enrol.class_id=classes.id "+rn "and classes.cour_id=course.id ";rn sqlBean db = new sqlBean();rn ResultSet rs= db.executeQuery(sql); rn return rs;rn rn //错误处理方法rn public void doError(HttpServletRequest req,rn HttpServletResponse res,rn String str)rn throws ServletException, IOException rnrnrn req.setAttribute("problem", str);rn RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");rn rd.forward(req, res);rn return;rn rn //将查询后的结果集存储在session中 rn public void sendResultSet(HttpServletRequest req, HttpServletResponse res,rn java.sql.ResultSet rs, String target)rn throws ServletException, IOException rn req.setAttribute("rs", rs);rn RequestDispatcher rd = getServletContext().getRequestDispatcher(target);rn rd.forward(req, res);rn return;rn rn//响应post请求rn public void doPost(HttpServletRequest req, HttpServletResponse res)rn throws ServletException, IOException rnrn doGet(req, res);rnrn rn rnrn rn rn rn rn rn

怎么会出现 Cannot forward after response has been committed???

02-20

下面这个是难用户的servlet:rnpackage com.wdj;rnimport java.io.*;rnimport javax.servlet.*;rnimport javax.servlet.http.*;rnpublic class CheckLogin extends HttpServletrn public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletExceptionrn User u=new User();rn String path=null;rn u.setName(request.getParameter("name"));rn u.setPass(request.getParameter("pass"));rn if(u.isValidate())rn path="main.jsp";rn elsern path="error.jsp";rn rn request.getRequestDispatcher(path).forward(request,response);rn rn public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletExceptionrn this.doGet(request,response); rn rn rnrnrnrn下面这个是过滤器;rnpackage com.wdj;rnimport java.io.*;rnimport javax.servlet.*;rnimport javax.servlet.http.*;rnpublic class UserFilter implements Filterrn public void init(FilterConfig congif)throws ServletExceptionrn rn rn public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletExceptionrn String name=request.getParameter("name");rn rn if(name!=null)rn if(name.indexOf("SEX")==-1)rn chain.doFilter(request,response);rn elsern System.out.println("error");rn request.getRequestDispatcher("error.jsp").forward(request,response);rn rn chain.doFilter(request,response);rn elsern chain.doFilter(request,response);rn rn rn public void destroy()rn rnrn我一提交表单到servlet中就提示 Cannot forward after response has been committed???rn请问我该怎么改呢,谢谢了,给大家拜个晚年了

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试