java.lang.IllegalStateException: Cannot forward after response has been committed

jjava.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.sxt.hotel.meeting.web.servlet.admin.AddCMeetServlet.doPost(AddCMeetServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:876)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:612)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1777)
at java.lang.Thread.run(Thread.java:722)

导致异常的代码:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {

			........一个大字(略)
			/**
			 * 6. 成功之后,将成功信息保存在request中
			 */
			request.setAttribute("msg", "添加成功");
			request.getRequestDispatcher(
					"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
					.forward(request, response);
		} catch (Exception e) {
			if (e instanceof FileSizeLimitExceededException) {
				request.setAttribute("msg", "上传文件的大小超出了50kb");
				request.getRequestDispatcher(
						"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
						.forward(request, response);
			}
			if(e instanceof HotelException){
				request.setAttribute("msg", e.getMessage());
				request.getRequestDispatcher(
						"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
						.forward(request, response);
			}
			request.setAttribute("msg", "系统繁忙,请稍后再试...");
			request.getRequestDispatcher(
					"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
					.forward(request, response);
		}
	}

  异常原因:  

  这个异常的意思是已有提交了,不能够再次转向。这个异常是由于多次提交或者页面中存在多个请求转发造成的,就是说在后台程序在return之前就执行了跳转或者执行了转发操作。

  异常解决办法:

  在转发语句之后,添加return语句,return ;

  如下代码解决问题:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			一个大字(略)
			/**
			 * 6. 成功之后,将成功信息保存在request中
			 */
			request.setAttribute("msg", "添加成功");
			request.getRequestDispatcher(
					"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
					.forward(request, response);
		} catch (Exception e) {
			if (e instanceof FileSizeLimitExceededException) {
				request.setAttribute("msg", "上传文件的大小超出了50kb");
				request.getRequestDispatcher(
						"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
						.forward(request, response);
				return ;
			}
			if(e instanceof HotelException){
				request.setAttribute("msg", e.getMessage());
				request.getRequestDispatcher(
						"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
						.forward(request, response);
				return ;
			}
			request.setAttribute("msg", "系统繁忙,请稍后再试...");
			request.getRequestDispatcher(
					"/adminjsps/admin/dailyfix/meetting/addcategories.jsp")
					.forward(request, response);
		}
	}

  

时间: 2024-09-30 21:01:26

java.lang.IllegalStateException: Cannot forward after response has been committed的相关文章

java.lang.IllegalStateException: getWriter() has already been called for this response

java.lang.IllegalStateException: getWriter() has already been called for this response 出现原因: 1.代码中有打开的Response.getWriter(),未关闭 2.再次使用了ServletOutputStream out = response.getOutputStream(); 通过查看代码,response中的usingWriter=true,想办法将该标志位设置为false. response.r

response.sendRedirect 报 java.lang.IllegalStateException 异常的解决思路

今天在进行代码开发的时候,出现了 java.lang.IllegalStateException异常,response.sendRedirect("./DEFAULT.html"); 这一条语句出错. 在网上进行查找之后,发现问题很有可能是因为response.sendRedirect方法执行了两次,其中一次肯定是项目框架里面的跳转,而我自己要进行跳转的话,就显得极其不方便,最后我把目光放到前端页面,在前端页面进行跳转. 一开始我的前端语句是:window.location.href=

有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常 java.lang.IllegalStateException: Cannot create a session after the response has been committed 原因1: Session 的创建语句: HttpSession seesion = request.getSession(); 之前有Response的输出语句. 应该把HttpSession seesion = request.getSession(); 放

java.lang.IllegalStateException: Cannot call sendError() after the response has been committe

1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exception java.lang.IllegalStateException:Cannot call sendError() after the response has been committed 2.问题分析 从提示错误的字面意思判断“Cannotcall sendError() after the r

使用printWrite 在jetty中报错 java.lang.IllegalStateException: STREAM

1 public void doFilter(ServletRequest request, ServletResponse response, 2 FilterChain chain) throws IOException, ServletException { 3 // TODO Auto-generated method stub 4 HttpServletRequest req=(HttpServletRequest)request; 5 HttpServletResponse res=

WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException

2015-01-06 18:23:17.160:WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException: STREAM||Class: org.mortbay.jetty.Response|File: Response.java|Method: getWriter|Line: 616 - org/mortbay/jetty/Response.java:616:-1 2015

java.lang.IllegalStateException

java.lang.IllegalStateExceptionorg.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)经过分析.查看jdk文档终于找到解决的办法,在response.sendRedirect()方法后加return语句即可,如下:response.sendRedirect(login.jsp);return null;原因是:在程序中两次调用了response.sendRe

Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this respo

1.错误描述 Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:648) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFa

批量下载的实现及java.lang.IllegalStateException异常

在工作流的一张表单里可能会有多个步骤上传附件,在用户的待办中往往会存在多条带有附件的任务,如果一一打开并且点击下载链接下载,不仅费时,而且繁琐,用户体验较差. OA系统采用的是FastDFS做为文件服务器,FastDFS的Java客户端提供了上传.下载等功能供调用. 在我之前的文章里对此有描述,目前已有的代码有对文件的批量上传功能,但下载的参数往往是针对单个文件.比如单个文件的下载方法如下: /**  * 文件下载  * @author chao.gao  * @date 2014-2-17 下