'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 1:[1]
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>
Solution 2:[2]
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
Solution 3:[3]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Eugene Retunsky |
| Solution 3 | M-Soley |
