views:

1828

answers:

2

The situation is as follows:

page.jsp?var[0]=foo&var[1]=bar

How can this be retrieved in an array in Java?

The following:

page.jsp?var=foo&var=bar

I know can be retrieved using request.getParameterValues("var")

Any solutions for the above though?

A: 
HashMap m = request.getParameterMap();
Set k = m.keySet();
Set v = m.entrySet();

That will get you a Map call m with K,V pairs and both a set of keys and set of values. You can iterate those sets almost like an array.

Hope this helps.

sal
+1  A: 
Map<Integer,String> index2value=new HashMap<Integer,String>();

for (Enumeration e = request.getParameterNames(); e.hasMoreElements() ;)
 {
 String param= e.nextElement().toString();
 if(!param.matches("var\[[0-9]+\]")) continue;
 int index= (here extract the numerical value....)
 index2value.put(index,request.getParameter(param));
 }

Hope this helps.

Pierre
I was hoping there would have been a more "automatic" way. But this will have to do. Thanks.
adnan.