I know I can put something in the web.xml like this
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
However the jsp page won’t show any contructive information since it won’t get what exactly the exception is. I know we can have different exceptions forwarded to different pages by various exception-type but that’s too much to write in web.xml. I hope one page is enough and another for handling errors like 404.
So how should I pass the exception information to the jsp page? Use session?
The ideal situation might be the page gets the exception info and show some relevant messages about it without revealing the exception to the users. Instead it could log it into a file for future reference. What is the best approach to achieve this? Thanks.
The information about the exception is already available by several request attributes. You can find the names of all those attributes in the
RequestDispatcherjavadoc:ERROR_EXCEPTION–javax.servlet.error.exeptionERROR_EXCEPTION_TYPE–javax.servlet.error.exception_typeERROR_MESSAGE–javax.servlet.error.messageERROR_REQUEST_URI–javax.servlet.error.request_uriERROR_SERVLET_NAME–javax.servlet.error.servlet_nameERROR_STATUS_CODE–javax.servlet.error.status_codeSo, in a nutshell, this JSP example should display all the possible exception detail:
Additionally, you could also show this useful information:
The concrete
Exceptioninstance itself is in the JSP only available as${exception}when you mark the page as an error page:Only if you’re using EL 2.2 or newer, then you can print its stacktrace as below:
Or if you’re not on EL 2.2 yet, then create a custom EL function for that:
Which is registered in
/WEB-INF/functions.tld:And can be used as
As to the logging of the exception, easiest place would be a filter which is mapped on an URL pattern of
/*and does basically the following: