尝试获取servlet中单选按钮的值时始终为null

时间:2014-03-20 17:25:32

标签: jsp servlets

我没有得到我的错误也许有人可以帮忙...这是我的jsp代码:

<html>  
  <body>
      <form action="UploadServlet" method="post" enctype="multipart/form-data">
          <fieldset>
              <legend>Read a CSV File</legend>
              <span class="help-block">Uploads the csv to the server and executes the checked task.</span>
              <input type="file" name="file" size="50" />
              <br>
              <br>
              <label class="radio">
                  <input type="radio" name="fileOptions" id="import" value="import" onchange="showRootDirectoryDiv()" checked>import : create tasks from csv import file
              </label>
              <label class="radio">
                  <input type="radio" name="fileOptions" id="create" value="create" onchange="showRootDirectoryDiv()">create : create directory from root-directory and import file
              </label>
              <label class="radio">
                  <input type="radio" name="fileOptions" id="differ" value="differ" onchange="showRootDirectoryDiv()">differ : differs tasks from csv import file to jira tasks of a specific FixVersion and creates a csv file with tasks, which are not in jira and creates a report about occurrence of all tasks (jQL-query is taken from properties)
              </label>
              <br>
              <div id="root-directory">
                  <label>Enter the root-directory</label>
                  <input type="text" name="root-directory-create">
              </div>
              <br>
              <button type="submit" class="btn">Upload File</button>
          </fieldset>
      </form>
  </body>
</html>

所以现在

 String uploadOption = request.getParameter("fileOptions");

应始终返回一个值,因为始终有一个选中的单选按钮(默认导入)。

但无论选择什么,我总是会变空?我没有把它的名字检查过5次,但我总是变空。

1 个答案:

答案 0 :(得分:1)

因为您正在使用enctype="multipart/form-data"。当你这样做时,request.getParameter不再有效。如果您使用的是Servlet 3.0或更高版本,则必须通过request.getPart检索请求中的所有内容,或者使用Apache Commons文件上载库从请求(而不仅仅是文件)中检索所有内容。