Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
greenlaw Avatar asked Apr 11 '12 18:04

greenlaw


People also ask

What is param name in web XML?

The optional context-param element declares a Web Application's servlet context initialization parameters. You set each context-param within a single context-param element, using <param-name> and <param-value> elements. You can access these parameters in your code using the javax.

What is the use of init param in web XML?

The init-param element within a filter or servlet definition element contains initialization parameters for that filter or servlet instance. These are distinct from context parameters, discussed in Section 4.7. Each init-param contains a param-name element and a param-value element.

What is init param in servlet?

You can specify initial parameter values for servlets in web modules during or after installation of an application onto a WebSphere® Application Server deployment target. The <param-value> values specified in <init-param> statements in the web. xml file of web modules are used by default.

What is the difference between context init parameter and servlet init parameter?

Servlet init parameters are for a single servlet only. Nothing outside that servlet can access that. It is declared inside the <servlet> tag of Deployment Descriptor, on the other hand context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter.


1 Answers

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> 
like image 78
Ramesh PVK Avatar answered Sep 28 '22 23:09

Ramesh PVK