Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple error-code configuration web.xml

I'd like to direct all errors to my Errorsevlet without specifying all the codes explicitly. Is there any way to do like that?

<error-page>
   <error-code>400</error-code>
   <location>/servlet/com.abc.servlet.ErrorServlet</location>
</error-page>

**And after reaching the ErrorServlet how can i get the stack trace of the error in the servlet. So that i can email the details when one error occurs. **

like image 883
coder247 Avatar asked Dec 10 '22 17:12

coder247


2 Answers

If you can upgrade, since Servlet 3.0 it's possible to have a generic error page for all errors, even those not caused by an exception (e.g. 404, 401, etc). Just omit the <error-code> or <exception-type> altogether so that you only have a <location>.

<error-page>
   <location>/errorServlet</location>
</error-page>

Note that I replaced the URL to avoid the use of Tomcat's builtin and deprecated InvokerServlet.

like image 60
BalusC Avatar answered Dec 12 '22 06:12

BalusC


You will need to specify all the desired codes explicitly, a wildcard mechanism is not supported. There are not that many codes, here is a full list.

To print out the stacktrace (e.g. in a comment, for debugging purposes), you could do something like this:

<%@ page isErrorPage="true" import="java.io.*"%>
<body>
<p>Sorry, there was an error.</p>
<!-- The full stacktrace follows:-->
<!-- 
<%
if (exception != null) {
    exception.printStackTrace(new PrintWriter(out));
}
%> 
-->
</body>
like image 43
lucrussell Avatar answered Dec 12 '22 06:12

lucrussell