ZK错误处理:初始请求与AJAX请求期间

时间:2013-02-26 21:52:31

标签: error-handling zk

我希望尽可能优雅地处理我的webapp中的错误 当然,我RTFM并将以下内容添加到我的web.xml文件中:

<!-- WEB-INF/web.xml -->
<error-page>
    <exception-type>java.lang.Throwable</exception-type>    
    <location>/error.zul</location>    
</error-page>

这是基本Java webapp error handling,效果很好。由于ZK是一个AJAXy框架,因此在呈现页面后可能会抛出错误,因此ZK也为此提供了自己的错误处理:

<!-- zk.xml -->
<error-page>
    <exception-type>java.lang.Throwable</exception-type>    
    <location>/zk/common/error.zul</location>    
</error-page>

这也很有效,然而,它们并不好玩。在AU请求期间抛出错误时,ZK错误消息显示为应该但是我得到以下stderr

Feb 26, 2013 10:37:04 PM org.zkoss.zk.ui.http.DHtmlLayoutServlet handleError:273
WARNING: Failed to load the error page: /zk/common/error.zul
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:168)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:267)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    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:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet auEngine threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    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:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet zkLoader threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    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:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/error.zul]
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    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.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    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:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)

同样,这是ZK特定错误处理程序(在/zk/common/error.zul)拦截错误的时候。根据堆栈跟踪,似乎错误传播到正常的Java webapp错误处理程序(/error.zul)。

如果首次呈现页面时发生错误,则会显示正常的错误处理程序(在`/error.zul'处),而不是按预期显示页面,不会记录任何问题。

有关如何解决此问题的任何想法? 可以/我应该在我的ZK错误处理程序上做些什么来阻止错误传播吗?

1 个答案:

答案 0 :(得分:0)

您可以使用Initiator处理ZK Loader呈现页面时发生的异常。使用的标签是

<?init class="com.foo.MyInitial"?>

您需要实施Initiator

希望这有帮助。

参考:Link