coder247
coder247

Reputation: 2943

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. **

Upvotes: 4

Views: 11615

Answers (4)

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

<error-page>
    <exception-type>java.lang.Throwable</exception-type> 
    <location>/servlet/com.ibm.eisa.servlet.ErrorServlet</location> 
</error-page>

Try this one, all of your errors will be caught(500's) not 404 etc

Upvotes: 0

Peter
Peter

Reputation: 616

I had same concern and after some research I have found out that unfortunately there is no clear requirement to support default error page in Servlet 3.0 specs.

It's misleading that "error-code" or "exception-type" are optional tags in XSD so we tend to consider that default error page will be the one without "error-code" and without "exception-type" tag.

Some application servers (e.g. GlassFish) behave as we wish, take default error page, then following the order of specific error pages they override default error page.

I also tested this on WebLogic 12c and I couldn't get it working as on GlassFish. Below article gives more clues about Tomcat.

See: bz.apache.org/bugzilla/show_bug.cgi?id=52135

Upvotes: 0

BalusC
BalusC

Reputation: 1108587

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.

Upvotes: 12

lucrussell
lucrussell

Reputation: 5150

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>

Upvotes: 2

Related Questions