简体   繁体   中英

How do I handle a posted String array in servlet?

In my form the user can check several checkboxes before posting the form to the servlet:

  <input type="checkbox" class="genre" name="genre[]" value="1"><label for="1">First Person Shooter</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="2"><label for="2">Sports</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="3"><label for="3">Action/Adventure</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="4"><label for="4">Educational</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="5"><label for="5">Puzzle</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="6"><label for="6">Real Time Strategy</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="7"><label for="7">Beat em ups</label><br>
  <input type="checkbox" class="genre" name="genre[]" value="8"><label for="8">Survival Horror</label><br>

I post the form data to the servlet using an AJAX call and serializing the form data like this:

$(".mainSurvey").submit(function(e){

    e.preventDefault(); //STOP default action
    var postData    = $(".mainSurvey").serializeArray();
    var botCatcher  = $("#botCatcher").val();

    if($(".mainSurvey input:checkbox:checked").length > 0 && botCatcher.length == 0){

        $.ajax(
        {
            type: "POST",
            url : "DatabaseService",
            data : postData,
            success: function(data) 
            {
                // continue
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                // handle error
            });

        }else{
            // handle error
        }
});

Where I would usually access a text inputs value using:

String input = request.getParameter("input");

How do I access the array of checkbox values within the servlet after posting?

From the Servlet API docs:

getParameter

public java.lang.String getParameter(java.lang.String name) Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String).

If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.

If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.

Parameters: name - a String specifying the name of the parameter Returns: a String representing the single value of the parameter See Also: getParameterValues(java.lang.String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM