35

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?

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
greenlaw
  • 867
  • 2
  • 9
  • 17

5 Answers5

46

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>
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
21

Put each param on its own line. I did the following recently and it works fine:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-beans.xml
        /WEB-INF/security-config.xml    
    </param-value>
</context-param>
stephen.hanson
  • 9,014
  • 2
  • 47
  • 53
  • 2
    So I assume you just split with a newline character and trimmed whitespace? This would definitely make it more readable. – greenlaw Apr 12 '12 at 13:20
  • 3
    This only works because spring splits the values for you in the ContextLoaderListener. From [their documentation](https://github.com/spring-projects/spring-framework/blob/f38a4b9040f50328ccc8c8c4fe54d366ff2c99ba/spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableConfigApplicationContext.java#L62): _Set the config locations for this application context in init-param style, i.e. with distinct locations separated by commas, semicolons or whitespace._ – Lucas Jul 11 '16 at 16:26
14

Yes, just use delimiters (as no other options available for this):

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



then simply
String[] validHosts = param.split(","); // not really much to do
Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
-1

You should use ; to separate them in param-value tag like:

<context-param>
  <param-name>validHosts</param-name>
  <param-value>example1.com;example2.com;.....</param-value>
</context-param>
M-Soley
  • 265
  • 1
  • 4
  • 13
-3

This works for me. The right delimiter to use is the ;