`
xiangxm
  • 浏览: 66948 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
  • xiangxm: xurichusheng 写道一些涉及到密码保存的就需要加密/ ...
    3DES加密
  • xurichusheng: 一些涉及到密码保存的就需要加密/解密了。不需要解密的密码,如登 ...
    3DES加密

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

阅读更多
问题描述:
java.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)
Dec 17, 2012 7:21:15 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet SelectServlet threw exception
java.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.servlet.SelectServlet.doPost(SelectServlet.java:110)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest

(ApplicationDispatcher.java:436)
	at org.apache.catalina.core.ApplicationDispatcher.doForward

(ApplicationDispatcher.java:374)
	at org.apache.catalina.core.ApplicationDispatcher.forward

(ApplicationDispatcher.java:302)
	at com.servlet.LoginServlet.doPost(LoginServlet.java:77)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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.Http11Processor.process(Http11Processor.java:861)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process

(Http11Protocol.java:606)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)
	at com.servlet.SelectServlet.doPost(SelectServlet.java:110)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest

(ApplicationDispatcher.java:436)
	at org.apache.catalina.core.ApplicationDispatcher.doForward

(ApplicationDispatcher.java:374)
	at org.apache.catalina.core.ApplicationDispatcher.forward

(ApplicationDispatcher.java:302)
	at com.servlet.LoginServlet.doPost(LoginServlet.java:77)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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.Http11Processor.process(Http11Processor.java:861)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process

(Http11Protocol.java:606)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)



错误重点截取:
org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet SelectServlet threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed


解决方案:

A common misunderstanding among starters is that they think that the call of a forward() or a sendRedirect() would magically exit and "jump" out of the method block, hereby ignoring the remnant of the code. For example:
protected void doPost() {
    if (someCondition) {
        sendRedirect();
    }
    forward(); // This is STILL invoked when someCondition is true!
}

This is thus actually not true. They do certainly not behave differently than any other Java methods (expect of System#exit() of course). When the someCondition in above example is true, then the chance is big that you will get an IllegalStateException: Cannot forward after response has been committed. This is regardless of whether the if statement calls a sendRedirect() or a forward().

To fix this, you need to add a return; statement afterwards
protected void doPost() {
    if (someCondition) {
        sendRedirect();
        return;
    }
    forward();
}

... or to introduce an else block.
protected void doPost() {
    if (someCondition) {
        sendRedirect();
    } else {
        forward();
    }
}

To naildown the root cause in your code, just search for any line which calls a sendRedirect() or a forward() without exiting the method block or skipping the remnant of the code. This can be inside the same servlet before the particular code line, but also in any servlet or filter which was been called before the particular servlet.

添加else语句或者添加return ;  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics