1

I've been used to do this in PHP where I can do like this in HTML:

<input name="a[b][c]" value="abc">
<input name="a[b][d]" value="def">
<input name="a[d][y]" value="nope">

Then get it in PHP e.g. as:

foreach($_POST['a']['b'] as $thisId => $value){
    echo "key: $thisId, value: $value\n";
}

Which outputs:

key: c, value: abc
key: d, value: def

I've tried to find but I can't seem to be able to find an equivalent in servlets. How can I get the equivalent with plain servlets (without me doing the code itself)?
If it doesn't exist, is there a library that does that job?

Edit: To add up to what sᴜʀᴇsʜ ᴀᴛᴛᴀ mentions, I do not want the list of everything that I get in the request... I added another element in the example above to try to make it more clear.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
brunoais
  • 6,258
  • 8
  • 39
  • 59
  • I didn't know exactly. see this link http://stackoverflow.com/questions/14677993/how-to-create-a-hashmap-with-two-keys-key-pair-value. – SatyaTNV Jul 16 '15 at 13:30
  • @Satya Nice try but no. I'd have to put the values there so I'd have to do the work regardless... – brunoais Jul 16 '15 at 14:04

1 Answers1

0

Are you looking something like this ?

    Map<String, String[]> paramValMap = req.getParameterMap();
  for (Map.Entry<String, String[]> param : paramValMap.entrySet()) {
    String key = (String) param.getKey();
    String[] values = (String[]) param.getValue();
    out.print(key);
    for (String val : values) {
            out.println(val);
    }
  }

That map contains, each parameter and an array of it's values.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • No. That gives me all the parameters sent through POST but there's no tree-like organization with them. Besides, if there an input with name="a[c][y]", I'd be screwed with the code above. – brunoais Jul 16 '15 at 12:31
  • Still the same kind of thing. It is a more useful API but it is still the single key-value instead of key-key-key-value – brunoais Jul 16 '15 at 12:35
  • Thanks for trying... it is actually still key-value... It's just that in PHP it's all hashmaps. – brunoais Jul 16 '15 at 13:06