How can I catch all errors to same page from web.xml?
Asked Answered
H

3

15

I tried to use

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errors/error.jsp</location>  
</error-page> 

but i dosen't catch 404 errors. How can I catch also 404 etc. errors to that same page ? I want to catch ALL error codes to same error page jsp.

Hakenkreuz answered 2/6, 2010 at 8:49 Comment(0)
S
10

You can add an <error-code> tag for that

<error-page>
    <error-code>404</error-code>
    <location>/errors/error.jsp</location>
</error-page> 

UPDATE:

As per your updated question - you'll have to define EACH error-code individually in the web.xml.

Using <exception-type>java.lang.Throwable</exception-type> will catch the error 500s but not 404s

Singlehandedly answered 2/6, 2010 at 8:53 Comment(4)
I know that, but I want to catch ALL error codes to same pageHakenkreuz
@Hakenkreuz - so you add an <error-page> for each and every error code, all of them pointing at the same page.Juncture
@StephenC:Isn't there any wildcard feature for the same? :/Raymund
According to the Servlet specification, no. (Check it for yourself.)Juncture
C
3

I am using this:

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/pages/exception/sorry.html</location>
</error-page>

<error-page>
    <error-code>403</error-code>
    <location>/security/403</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/security/404</location>
</error-page>
Carter answered 20/11, 2013 at 9:32 Comment(0)
K
1

In Tomcat7 (might work on older versions but I didn't check)

add the error pages you want (e.g 404.jsp, general_error.php etc.)

add to web.xml (all first and then specific. adapt it to your code of course):

<error-page>
    <location>general_error.php</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>404.jsp</location>
</error-page>

<error-page>
    <error-code>409</error-code>
    <location>error_page.jsp?error=custom_message</location>
</error-page>
Kattegat answered 1/5, 2013 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.