url-mapping for all .html URLs, including URLs with query parameters

2.5k Views Asked by At

I'm trying to write a filter that will pick out any request for an HTML page, regardless of whether or not the URL has query parameters. (The aim of this filter is to track visitor actions/stats)

This is filter mapping I have in my web.xml:

<filter-mapping>
    <filter-name>statTrackingFilter</filter-name>
    <url-pattern>*.html</url-pattern>
</filter-mapping>

That url-pattern will match requests for pages ending in ".html", but I'd also like to capture requests with query parameters.

I've tried the following configuration, but it doesn't match any requests ever:

<filter-mapping>
    <filter-name>statTrackingFilter</filter-name>
    <url-pattern>*.html*</url-pattern>
</filter-mapping>

How can I configure my filter to capture all requests to any HTML page?

Or am I going about this the wrong way? Should I just make my filter listen for all requests and let the filter decide if the requested URL is important? I want to ignore all CSS, JS and image requests.

Thank you.

Edit: <url-pattern>*.html</url-pattern> was indeed working. I thought it wasn't because the logs from my filter were being ignored due to my log4j threshold being set too high.

1

There are 1 best solutions below

1
On BEST ANSWER

The *.html* pattern won't work because it will only match files having an extension literally named ".html*".

From link:

URL patterns use an extremely simple syntax. Every character in a pattern must match the corresponding character in the URL path exactly, with two exceptions. At the end of a pattern, /* matches any sequence of characters from that point forward. The pattern *.extension matches any file name ending with extension. No other wildcards are supported, and an asterisk at any other position in the pattern is not a wildcard.

I believe your first configuration using <url-pattern>*.html</url-pattern> was correct and should capture requests with query parameters at the end as well (it seems to for me anyways).