4

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?

2
  • Does it have to be in a GET request? Commented May 13, 2009 at 20:16
  • No, it can be POST or GET, as long as it's retrievable via the request object. Commented May 14, 2009 at 12:50

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping there would have been a more "automatic" way. But this will have to do. Thanks.
param.matches("var[[0-9]+]") does not compile. Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
4
HashMap m = request.getParameterMap();
Set k = m.keySet();
Set v = m.entrySet();
Object o[] = m.entrySet().toArray();

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. You can also use toArray to turn it into an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.