简体   繁体   中英

errorPage directive works, but error-page in web.xml doesn't?

I get jsp exceptions causing a forward to my error page when I put this at the top of my JSPs...

<%@ page errorPage="/error.page" %>

but when I try to do it globally with web.xml like so:

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error.page</location>
</error-page>

I just get a blank page... I've also tried putting /error.jsp in the location element.. but no love with that either..

I am triggering an exception with a jsp that just contains this:

<%if(true)throw new RuntimeException("test exception");%>

I do see the exception in the console from tomcat but I just can't get that error page to show without a directive on every jsp... am I missing something simple here?

UPDATE:

/error.page is mapped (using spring) the contents are this:

<%@ page isErrorPage="true"%>
<html>
<head></head>
<body>
<div class="error">
An error has occurred, the development team has been notified. Sorry for the inconvenience.
</div>
</body>
</html>

I can hit the page directly with no error.

UPDATE:

If you have this problem... make sure you don't have filters swallowing exceptions in your chain! see my answer below.

Make sure that if /error.page maps to a servlet, that Servlet implements all relevant do methods ( at least doPost and doGet ).

I had faced a similar problem ( blank page, nothing displayed ), because in my initial implementation I've only implemented doGet .

The actual do calls can converge on a single method, because handling of the errors is very similar in most cases.

Unreal... it turns out there was a filter in the chain that was swallowing and spitting out the exception... so it never propagated up high enough to be handled by the container! bad form! I took that out and now everything works as expected... sorry for the noise.

make sure /error.page is valid mapping. If your error page itself has errors it will cause tomcat to show its generic error page

Usually, if you are using /ErrorPage.jsp or /ErrorPage you need to have a JSP file in your webapp/appname/ directory or Servlet (with the correct url-mapping in Deployment Descriptor)

Also, if you are using the directive as well as the DD, the directive will always take precedence.

As a suggestion, rather than just display a friendly message to the end user, you should also consider doing something useful with the exception, such as logging it or notifying someone that it occurred.

You can do this by accessing various attributes set in the request, such as:

request.getAttribute("javax.servlet.error.message");

and using these attributes in a bean or an inline JSP codeblock.

Check out http://metatations.com/2011/11/20/global-exception-handling-in-java/ for a more detailed explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM