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

Posted GEORES

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
		}
	}

  

以上是关于java.lang.IllegalStateException: Cannot forward after response has been committed的主要内容,如果未能解决你的问题,请参考以下文章