Is it possible for a Servlet filter to retrieve its url path?

I can map a single servlet to various url patterns in the web.xml file. When programming the servlet, I can then get which of those url patterns the request is matching, by getting the servlet path through request.getServletPath().

How can I achieve this with filters? When mapping a filter to various url patterns, is there a way to get what path the current request is matching?

Because my filter is currently working on content that is mapped to the DefaultServlet, the request.getServletPath() returns the whole path, and PathInfo is always null.

I am a newbie on servlet and filters, so I hope my question is clear and makes sense at all.

Answer

No, there isn’t. You have to determine it yourself based on the request URI and a predefinied set/list/map of all known/supported paths. You can if necessary set those paths as <init-param> of the filter and process it during the init() method so that you can reuse it in the doFilter() method.

Leave a Reply

Your email address will not be published. Required fields are marked *