【发布时间】:2011-01-19 11:04:02
【问题描述】:
来自this 的帖子我想到了另一个问题,我似乎无法在互联网上找到答案。
我有一个 servlet 过滤器,它检测会话超时并重定向到错误页面。由于我刚刚起步的 java,我使用 try catch 块来捕获异常并优雅地处理它们,但是在会话超时情况下,上下文是无效的,所以我认为我没有任何代码可以做到这一点停止出现在我的日志中的 'viewId 无法恢复' stace 跟踪,即使我有一个过滤器处理它。
如何阻止异常跟踪出现在我的日志中?它将在现场自动进行监控,如果出现此类错误,则会向支持团队发出误报。
任何帮助表示赞赏。
编辑
为了更清楚,我的代码片段目前是
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (ServletException e) {
logger.error("Caught Servlet Exception");
Throwable rootCause = e.getRootCause();
logger.error("Root cause is " + rootCause.toString());
if (rootCause instanceof RuntimeException) { // This is true for any FacesException.
logger.error("Rethrowing exception as RuntimeException" + rootCause.toString());
throw (RuntimeException) rootCause; // Throw wrapped RuntimeException instead of ServletException.
} else {
throw e;
}
}
}
日志说:
|STDOUT| 2011-01-19 10:40:57,803 | ERROR | [http-8080-5]: Exception in the filter chain
javax.servlet.ServletException: viewId:/index.jsf - View /index.jsf could not be restored.
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:270)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
<snip>
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.application.ViewExpiredException: viewId:/index.jsf - View /index.jsf could not be restored.
<snip>
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
... 21 more
19-Jan-2011 10:40:57 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.application.ViewExpiredException: viewId:/index.jsf - View /index.jsf could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:185)
<snip>
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
|STDOUT| 2011-01-19 10:40:57,803 | ERROR | [http-8080-5]: Caught Servlet Exception
|STDOUT| 2011-01-19 10:40:57,803 | ERROR | [http-8080-5]: Root cause is javax.faces.application.ViewExpiredException: viewId:/index.jsf - View /index.jsf could not be restored.
|STDOUT| 2011-01-19 10:40:57,803 | ERROR | [http-8080-5]: Rethrowing exception as RuntimeExceptionjavax.faces.application.ViewExpiredException: viewId:/index.jsf - View /index.jsf could not be restored.
从代码中可以看出,我已经在过滤器中捕获到了servlet异常,但是堆栈仍然出现在日志中。
下一个编辑 我的 web.xml 中的完整过滤器列表如下:
<filter>
<filter-name>Error</filter-name>
<filter-class>prismClient.ErrorFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Error</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
【问题讨论】:
标签: java servlets stack-trace servlet-filters