`

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

阅读更多
今天查看应用程序的日志文件, 发现其中反复的出现如下的异常:
Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:313)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
        at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
        at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
        at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at AuthorizationFilter.doFilter(AuthorizationFilter.java:66)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)


        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:595)


然后在网上查看了一些相关的贴子, 基本上的意思就是 response 已经失效之后,又对response进行了调用, 因为response的状态已经改变了 再次调用就会触发这个异常.

然后对代码进行查看,发现了问题:
在上面的日志中大家可以找到这一行: AuthorizationFilter.doFilter(AuthorizationFilter.java:66)
源文件中对应代码如下:
chain.doFilter(request, response);

在此次对response调用之前,有如下的调用:
config.getServletContext().getRequestDispatcher("/WEB-INF/pages/Error.jsp").forward(request, response);

在这个时候 response 就被提交了.
再执行到
chain.doFilter(request, response);

的时候,response has been committed.

解决办法:在第一次forward之后,加一条return;语句. 原理就是不要类似以上的2条语句,
同时执行到,也就是不要在response已经提交后 再次对response进行调用.

类似情况:
Response.sendRedirect();后没有加return语句.
分享到:
评论
1 楼 pro_bin 2011-08-30  
我同样的问题,一个调用不抱错,一个调用报错了,加了return了的

相关推荐

Global site tag (gtag.js) - Google Analytics