输入单选按钮值在servlet中不返回任何内容

输入单选按钮值在servlet中不返回任何内容

问题描述:

我正在尝试将dataquery1的值传递给servlet,但是它什么也不返回. 这是我在jsp中的代码:

I'm trying to pass value of dataquery1 to a servlet but it returns nothing. Here is my code in jsp :

<form id="formquery" action="queryD" method="post">
  <div>Search for : &nbsp;&nbsp; 

        <select id="option" class="enjoy-css" style="width:170px" name="optdata">                       
    <option>Choose options</option>
    <option value="scname">Scientific Name</option>                     
    <option value="fname">Family Name</option>
      <option value="location">Location</option>
      <option value="wateruse">Water Usage</option>
    </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input class="enjoy-css" type="submit" name="Search" value="Search">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a style="text-decoration: underline" onclick="resetQuery()">Reset</a><br/>
    <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <div id="opt1" style="display:none;">
    Keyword : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input class="enjoy-css" style="width:120px" placeholder="Enter keyword" type="text" name="dataquery1">
    <br><br>    
    </div>

    <br/>
    <div id="opt2" style="display:none;">
    <input type="radio" name="dataquery1" value="DTC"> DTC<br>
    <input type="radio" name="dataquery1" value="Engineering"> Engineering<br>
    <input type="radio" name="dataquery1" value="Business"> Business<br>
    <input type="radio" name="dataquery1" value="Science"> Science<br>
    <input type="radio" name="dataquery1" value="Law"> Law<br>
    </div>

    <br/>
    <div id="opt3" style="display:none;">
    <input type="radio" name="dataquery1" value="Low"> Low<br>
    <input type="radio" name="dataquery1" value="Low to moderate"> Low to moderate<br>
    <input type="radio" name="dataquery1" value="Moderate to high"> Moderate to high<br>
    <input type="radio" name="dataquery1" value="High"> High<br>
    </div>

  </div>
  </form>

这是我的servlet中的

And here is in my servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("queryD.java");

  //get query from newquery.jsp
  String dataquery1 = "";
  dataquery1 = request.getParameter("dataquery1");

  //get option from newquery.jsp
  String optdata = "";
  optdata = request.getParameter("optdata");

  System.out.println("OPTDATA : " + optdata + " | KEYWORD : " + dataquery1 + ".");

对于opt1 div,它可以完美运行.但是对于opt2opt3,它什么也不返回.我试图将广播输入更改为文本输入,但仍然无法正常工作.

For opt1 div, it works perfectly. But for opt2 and opt3 it returns nothing. I tried to change from radio input to text input but it still doesn't work.

PS:opt1, opt2, opt3显示器设置为none,但我具有调用它的功能.

PS: opt1, opt2, opt3 display is set to none but i have the function that call for it.

我在自己的IDE中尝试了您的代码,并重现了您的问题.问题是这样的.您的复选框和1个文本输入都具有相同的name属性,因此,当将其发送到servlet时,您得到的是一个元素数组("dataquery1")

I tried your code in my own IDE and reproduced your problem. The problem is this. Your checkboxes and 1 text input all have the same name attribute, so when you send this to your servlet, what you get is an array of elements ("dataquery1")!

如果将关键字"输入保留为空,则尝试通过servlet访问它时将看不到任何内容.因为您在需要访问数组时尝试访问String值.如果您不将关键字"输入保留为空,并尝试获取"dataquery1"的字符串值,则将自动获取数组的第一个值.

if you leave the "keyword" input empty, you will see nothing when you try and access it via the servlet. Because you are trying to access a String value when you need to access the array. If you do not leave the "keyword" input empty and try to get the String value of "dataquery1", you will automatically get the first value of the array.

您的表单:

**要调试的网络标签(查看发送的内容):**

**The network tab to debug (to see what gets sent): **

要访问变量,您必须使用数组. (因为"dataquery1"不是唯一的")签出

To access your variables you have to use an array. (because "dataquery1" is not unique") Check out this answer for further information on that.

这是您的操作方式:

String[] test = request.getParameterValues("dataquery1");
String value1 = test[0];
String value2 = test[1];