web.xml error page not filtered
Asked Answered
C

1

5

My application is running on Tomcat 7.

I've created a url rewrite filter that is listening on all incoming request, yet, when an error page is triggered, it doesn't filter it, instead it does filter the page on which the error occured.

I set up a breakpoint in the filter and when the error occurs, you can see it triggers on the source page. But the displayed page is /desktop/index.xhtml

Is it the expected behavior ?

Here is my web.xml configuration :

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<error-page>
    <error-code>500</error-code>
    <location>/desktop/index.xhtml?messageId=4</location>
</error-page>
Chloechloette answered 19/1, 2015 at 14:48 Comment(0)
R
11

Is it the expected behavior ?

Yes.

Filters are by default mapped on the REQUEST dispatcher only. The below

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

is equivalent to

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

This means, the filter is only triggered on the "raw" incoming request, not on forwarded request or an error page request.

There are two other dispatchers: FORWARD and ERROR. Error pages are internally dispatched via the ERROR dispatcher. If you'd like to let your filter hook on that as well, then add it:

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

Note that you need to explicitly specify the REQUEST dispatcher here, otherwise it would assume that you're overriding it altogether and are only interested in ERROR dispatcher.

Inside the filter, you can then check by the presence of a request attribute keyed with RequestDispatcher#ERROR_REQUEST_URI if it was being triggered or not.

String errorRequestURI = (String) request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);

if (errorRequestURI != null) {
    // Error page was triggered on the given URI.
}
Rufena answered 19/1, 2015 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.