Jonathan Laliberte Jonathan Laliberte - 4 years ago 151
Java Question

Get Array from multiple select using isFormField stream / multipart form. Java Servlet

Trying to get the values from a multiple select field. But only able to retrieve the first value?

In a normal form, have no problem iterating over the field name with a for-loop and then adding the values to an array.

But when using a Multipart request form, having trouble understanding how exactly it would be possible to get those same values?

For instance,

servlet:

String sctype = null, sfieldname = null, sname = null;
FileItemIterator iterator;
FileItemStream item = null;
InputStream stream = null;
try{

ServletFileUpload upload = new ServletFileUpload();
iterator = upload.getItemIterator(request);

while (iterator.hasNext()) { // iterate over form fields

item = iterator.next();
stream = item.openStream();
String fieldname = item.getFieldName();

if (item.isFormField()) { // Problem here

String value = Streams.asString(stream);

//String[] valueArray = Streams.asString(stream) //test

if (fieldname.equals("title")){

title = value;
}

if (fieldname.equals("multipleSelect")){

//multipleSelect = valueArray[]; //test
multipleSelect = value; // only gives one value :S

} else { // gets values from uploaded files

sfieldname = item.getFieldName();
sname = item.getName();
sctype = item.getContentType();

stream.close();

}

} // if form
} // while

} ....( try catch block/finally, etc)...


jsp/html

<form action="FormServlet" method="post" enctype="multipart/form-data">

<textarea class="form-control" name="title" placeholder="Title"></textarea>

<select class="form-control" name="multipleSelect" multiple="multiple">
<option value="ring">Ring</option>
<option value="necklace">Necklace</option>
</select>

<input type="file" name="file1" size="50" multiple>

</form>


Hope that was clear. Any pointers much appreciated. Thanks for your time.

Answer Source

Here is a simple workaround which I have used

  1. Create a hidden text field between <form> and </form> for storing the selected item values of the dropdown as a delimited string (a comma in this example). (i.e:'ring,necklace')

    <input type="hidden" id="multipleSelectValues" name="multipleSelectValues" />
    
  2. Add client-side JavaScript/JQuery..etc so that when the selected values are changed, the text in the hidden text field would update.

    $("select[name='multipleSelect']").change(function() {
        var arr = $("select[name='multipleSelect']").val(); //automatically creates an array of selected values
        var foo = arr.join(","); //creates a comma delimited string (i.e:'ring,necklace')
        $( "#multipleSelectValues" ).val(foo); //update hidden field value
    });
    
  3. At the servlet, the values can be parsed using string split() command.

    if (fieldname.equals("multipleSelect")){
        String[] valueArray = value.split(","); //split string by ,
        //do your array stuff here, for example
        for (String individualValue: valueArray ) {           
            //play with individual dropdown item here, for example
            System.out.println(individualValue);
        }
    }
    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download