How to get request parameter to an array in Java, in the style of PHP and Rails?

The situation is as follows:

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

How can this be obtained in an array in Java?

Following:

 page.jsp?var=foo&var=bar 

I know what can be obtained with request.getParameterValues ​​("var")

Any solutions for the above though?

+4
source share
2 answers
 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.

+4
source
 HashMap m = request.getParameterMap(); Set k = m.keySet(); Set v = m.entrySet(); Object o[] = m.entrySet().toArray(); 

This will give you a call to map m with pairs K, V and a set of keys and a set of values. You can repeat these sets almost like an array. You can also use toArray to turn it into an array.

+4
source

All Articles