I wrote a servlet to handle the exceptions occurring in my web app and mapped them in web.xml
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/exceptionHandler</location>
</error-page>
Here is what I have done in the Exception Handling servlet service
method:
@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);
}
}.
Problem:
The above approach is not working and the stack trace is printing to the console. This occurs when the user requests something and then closes their browser.
Question:
How do I stop printing the stacktrace to the JBoss console whenever a SocketException
occurs?
Reason for doing this:
I want to avoid seeing all of the log's SocketException
s at the end of th day because I can't do anything with that information.
Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.
A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.
As someone who has to troubleshoot regularly, I would never omit printing a stacktrace when something has gone wrong. Yes, don't show it to the user, but yes, dump it to an error log.
Here is what I done so war as work around.
Added one filter and hijack all the request and response.Catch the exception and check the type.
/**
* 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.
}
}
}
And in web.xml
,filter mapping
<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>
I'm not marking this as answer,since I'm not sure this is a standard way or not.Just a work around.
Instead of adding java.lang.Exception
in your web.xml why won't you just try to add socket exception in web.xml itself like below
<error-page>
<exception-type>java.net.SocketException</exception-type>
<location>/exceptionHandler</location>
</error-page>
and just do nothing in your servlet
Or instead add a empty jsp file like
<error-page>
<exception-type>java.net.SocketException</exception-type>
<location>/error.jsp</location>
</error-page>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With