Search code examples
javaservletsparametersservlet-filtersmultiple-value

Java servlet Filter with multiple values in FilterConfig? Is it possible?


I'm trying to implement a servlet filter that would ignore some URL's while filtering everything else. Wanting to make it flexible, I tried to set the excluded URL's as FilterConfig params. Though, in the server configuration, the filter params section doesn't seem to accept multiple values for a given param name, so I'm kind of stuck wondering if and how to include several values that I can then receive as a Set or Array in the filter init().

Here's basically what I'm after:

<filter> <filter-name>RequestFilter</filter-name> <filter-class>...RequestFilter</filter-class> <init-param>
<param-name>ignoredUrls</param-name>
<param-value>/url1</param-value> <param-value>/url2</param-value> <param-value>/url3</param-value> </init-param>
</filter>

Of course I can use a delimiter-splitter approach, but I'm wondering if there is some sort of standardized way of doing that.

Thanks a lot! Alex


Solution

  • Servlet spec says that you can have only one value for any context parameter. So, you are left with going with delimited list only.

    You may use some separator as ','

    <filter>
      <filter-name>RequestFilter</filter-name>
      <filter-class>...RequestFilter</filter-class>
      <init-param>
        <param-name>ignoredUrls</param-name>
        <param-value>/url1,/url2,/url3</param-value>
      </init-param>
    </filter>
    

    Later you would read these values from filter config in this way:

    String[] ignoredUrls = (param!=null)? param.split(",") : {}; // or something like this