jeudi 31 mars 2016

How to post multiple as array to Java servlet?

This question is the same as How to post multiple <input type="checkbox" /> as array in PHP?, but I can't make the solution work in my java servlet setup. When using the apporach of adding a [] to the name property of the grouped checkboxes, I only get the first checked option. I am not sure if it is the actual posted array that is containing only one element, or if I'm not accessing it right server side. Here is what I do to check the value in java:

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {  
    for(String name : request.getParameterMap().keySet()){      
        System.out.println(name +": " + request.getParameter(name));        
    }
}

this prints countries[]: US, even if I have more checboxes checked after the US-input. the value changes after which checkbox is the first of the checked ones. What am I doing wrong?

Here is my HTML:

<form action="mypage" method="post">
    <input id="cb-country-gb" type="checkbox" name="countries[]" class="hide" value="GB"/>
    <input id="cb-country-us" type="checkbox" name="countries[]" class="hide" value="US"/>
    <input id="cb-country-ge" type="checkbox" name="countries[]" class="hide" value="GE"/>
    <input id="cb-country-es" type="checkbox" name="countries[]" class="hide" value="ES"/>
    <button type="submit" class="btn btn-primary">Search</button>
</form>




Aucun commentaire:

Enregistrer un commentaire