[JSP] Obtaining a set of array values from a enctype multipart form.

Discussion in 'Mixed Languages' started by gravitunex, Jun 23, 2013.

  1. gravitunex

    gravitunex MDL Novice

    Aug 15, 2008
    34
    0
    0
    #1 gravitunex, Jun 23, 2013
    Last edited by a moderator: Apr 20, 2017
    A checkbox will have multiple values if the user checks more than one option. I am trying to retrieve the array of checked values.

    Previously, without the enctype multipart, a getParameterValues() would have done the job.

    I am using enctype multipart because there's a file upload option involved. I am using the Apache FileUpload API.

    Logic :
    I did a check if it is a form field, and then as long as the form field name matches the name of the checkbox 'genreList' , it will keep extracting the checkbox parameters out.

    The checkbox params are then inserted into a ArrayList for further manipulation.

    However, my current method of extracting the multiple checkbox values doesn't work.

    Any help is very much appreciated :worthy::worthy:

    Relevant HTML code :

    Code:
    <form  action="GetData.jsp" method="post" enctype="multipart/form-data">
    
    <!--Normal Form Fields -->
    <td>Release Date</td>
    <td><input type='text' size=30 name='date'></td>
    
    <td>Apple</td>
    <td><input type='text' size=30 name='apple'></td>
    
    <!-------------------------- Checkbox ---------------------------------------->
    <td>Genre:</td>
    <td><input type='checkbox' name='genreList'> " + genreName + " </td>
    
    </form>
    
    Relevant JSP code:

    Code:
    String getRelDate = "";
    String getApple = "";
    
          try{ 
             // Parse the request to get file items.
             List<FileItem> items = upload.parseRequest(request);
    
             // Process the uploaded file items
             Iterator<FileItem> i = items.iterator();
    
             while ( i.hasNext () ) 
             {
            FileItem fi = (FileItem) i.next();
            
    if ( fi.isFormField () ){
    fi.getFieldName().equals("date");
    getRelDate = fi.getString();
    
    fi.getFieldName().equals("apple");
    getApple = fi.getString();
    
    // get params from checkbox named 'genreList'   
    // This doesn't work. 
    fi.getFieldName().equals("genreList");
    String getGenre = fi.getString("genreList");
    // adds each checkbox param to ArrayList 'genreTick' 
    genreTick.add(getGenre);
    }
    
    
        }
    }