Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.xml error page not filtered

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>
like image 841
madgangmixers Avatar asked Dec 24 '22 23:12

madgangmixers


1 Answers

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.
}
like image 160
BalusC Avatar answered Feb 24 '23 04:02

BalusC