0

I have the following code

@WebFilter(urlPatterns = "/path/to/directory/*")
public class PageFilter extends MyBaseFilter {
}

It works as expected if I do "http://localhost/path/to/directory", but if I do http://localhost/PATH/to/directory" or any other combination the filter does not work. It appears to be case sensitive. Is there a way to make this case insensitive so it matches any permutation of the url?

BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
asked Aug 29, 2024 at 22:36
1
  • Servlet paths are generally case-sensitive, so this should be expected. And it should not harm since, if a web resource listens to /path, it should not respond to /PATH unless specifically configured to do so. That said, I have seen in the past cases where a servlet container deployed on a case-insensitive file system (read: Windows), would accept case-insensitive URIs for the resources that exist in the file system. If you really want a case insensitive filter, catch the root path and decide programmatically whether to apply the filter logic or not. No JEE-native way. Commented Aug 30, 2024 at 8:16

1 Answer 1

1

Is there a way to make this case insensitive so it matches any permutation of the url?

No.

But you can add another filter which sends a 301 (Permanent) redirect to a lowercased URI when the URI contains an uppercased character.

E.g.

@WebFilter("/*")
public class LowerCasedUriFilter extends HttpFilter {
 @Override
 public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
 var uri = request.getRequestURI();
 var lowerCasedUri = uri.toLowerCase();
 if (uri.equals(lowerCasedUri)) {
 chain.doFilter(request, response);
 }
 else {
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 response.setHeader("Location", lowerCasedUri);
 }
 }
}

Make sure this filter is registered before yours.

answered Aug 30, 2024 at 10:24
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.