简体   繁体   English

如何处理Servlet中发布的String数组?

[英]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: 在我的表单中,用户可以在将表单发布到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: 我使用AJAX调用将表单数据发布到servlet并像这样序列化表单数据:

$(".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? 发布后如何访问servlet中的复选框值数组?

From the Servlet API docs: 从Servlet API文档中:

getParameter 的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. public java.lang.String getParameter(java.lang.String name)以字符串形式返回请求参数的值,如果该参数不存在,则返回null。 Request parameters are extra information sent with the request. 请求参数是与请求一起发送的额外信息。 For HTTP servlets, parameters are contained in the query string or posted form data. 对于HTTP Servlet,参数包含在查询字符串或发布的表单数据中。 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). 如果参数可能具有多个值,请使用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. 如果将此方法与多值参数一起使用,则返回的值等于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. 如果参数数据是在请求正文中发送的(例如在HTTP POST请求中发生),则直接通过getInputStream()或getReader()读取正文会干扰此方法的执行。

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) 参数:name-指定参数名称的String返回:表示参数的单个值的String另请参见:getParameterValues(java.lang.String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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