Search code examples
javaweb-applicationsservletsweb.xml

Java Servlets - Storing a list of values in web.xml (multiple param-value's for single param-name)


I'm creating a servlet that needs to load configuration information. Part of the configuration information I need is a list of Strings (specifically, a list of hostnames and/or URLs).

I was hoping to store this information in my servlet's web.xml file (so I don't have to write my own parser) as either a context-param or init-param; essentially multiple param-value's for a single param-name.

Example of what I would like:

<context-param>
    <param-name>validHosts</param-name>
    <param-value>example1.com</param-value>
    <param-value>example2.com</param-value>
    <param-value>example3.com</param-value>
</context-param>

My initial research seems to indicate this is not possible--that there can only be a single param-value for any param-name (within either context-param or init-param).

I know I could just use a delimited list within a single param-value, but is that really my only option if I still want to use web.xml? Should I just stop whining and write my own config file parser?


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.

    <context-param>
      <param-name>validHosts</param-name>
      <param-value>example1.com,example2.com,.....</param-value>
    </context-param>