Sur*_*tta 19 java tomcat web.xml servlets socketexception
我编写了一个servlet来处理我的Web应用程序中发生的异常并将它们映射到web.xml中
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exceptionHandler</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
这是我在异常处理servlet service
方法中所做的:
@Override
protected void service(HttpServletRequest req, HttpServletResponse arg1)
throws ServletException, IOException {
Object attribute = req.getAttribute("javax.servlet.error.exception");
if(attribute instanceof SocketException){
// don't do anything
}else{
super.service(req, arg1);
}
}.
Run Code Online (Sandbox Code Playgroud)
问题:
上述方法不起作用,堆栈跟踪正在打印到控制台.当用户请求某些内容然后关闭其浏览器时会发生这种情况.
题:
每当SocketException
发生时,如何停止将printtrace打印到JBoss控制台?
这样做的原因:
我想避免SocketException
在第一天结束时看到所有日志,因为我无法对这些信息做任何事情.
这就是我在战争中所做的工作.
添加了一个过滤器并劫持了所有请求和响应.捕获异常并检查类型.
/**
* Hijacks all the http request and response here.
* Catch the SocketException and do not print
* If other exceptions print to console
* date : 9-18-2013
*
* @author Suresh Atta
*
*/
public class ExceptionHandler implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
try{
arg2.doFilter(arg0, arg1);
}catch(SocketException e ){
// Please don't print this to log.
}
}
}
Run Code Online (Sandbox Code Playgroud)
在web.xml
,过滤映射
<filter>
<filter-name>ExceptionHandler</filter-name>
<filter-class>com.nextenders.server.ExceptionHandler</filter-class>
</filter>
<filter-mapping>
<filter-name>ExceptionHandler</filter-name>
<dispatcher> REQUEST </dispatcher>
<url-pattern> /*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)
我不是把它标记为答案,因为我不确定这是否是标准方法.只是一个解决方法.
不要java.lang.Exception
在web.xml 中添加,为什么不尝试在web.xml本身中添加套接字异常,如下所示
<error-page>
<exception-type>java.net.SocketException</exception-type>
<location>/exceptionHandler</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
并且只是在你的servlet中什么都不做
或者改为添加一个空的jsp文件
<error-page>
<exception-type>java.net.SocketException</exception-type>
<location>/error.jsp</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
您可能必须重写 fillInStackTrade 方法
public static class CustomException extends Exception {
@Override
public Throwable fillInStackTrace() {
return null;
}
Run Code Online (Sandbox Code Playgroud)
}