为什么POST参数不取值?

问题描述:

我是jsp的新手,我创建了第一个jsp页面。 在这里我想设置用户给定的值以显示在下面的文本框中。但是在提交之后,那些只需要空值。 我的代码如下所示。为什么POST参数不取值?

<form action="index.jsp" method="post"> 
      <table> 
       <tr> 
        <td>Country</td> 
        <td><input id="countryText" type="text" > </td> 
       </tr> 
       <tr> 
        <td>City</td> 
        <td><input id="cityText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>Check in Date</td> 
        <td><input id="checkinText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>No of Nights</td> 
        <td><input id="noOfNightsText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>No of Rooms</td> 
        <td><input id="noOfRoomsText" value="1" type="text" disabled></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td align="right"><button id="searchButton" type="submit" class="buttonClass">Search Hotels</button></td> 
       </tr> 
      </table> 
     </form> 

     <table> 
      <tr> 
       <td>Country</td> 
       <td><input id="countryTextOutput" value="<%= request.getParameter("countryText")%>" type="text" disabled> </td> 
      </tr> 
      <tr> 
       <td>City</td> 
       <td><input id="cityTextOutput" value="<%= request.getParameter("cityText")%>" type="text" disabled></td> 
      </tr> 
      <tr> 
       <td>Check in Date</td> 
       <td><input id="checkinTextOutput" value="<%= request.getParameter("checkinText")%>" type="text" disabled></td> 
      </tr> 
      <tr> 
       <td>No of Nights</td> 
       <td><input id="noOfNightsTextOutput" value="<%= request.getParameter("noOfNightsText")%>" type="text" disabled></td> 
      </tr> 
      <tr> 
       <td>No of Rooms</td> 
       <td><input id="noOfRoomsTextOutput" value="<%= request.getParameter("noOfRoomsText")%>" type="text" disabled></td> 
      </tr> 
     </table> 

我的Web界面 Web Interface

在此先感谢..!

+2

此外,请尝试为您的输入添加名称属性。 – thexacre

+0

@mit:这应该是一个答案(如果你详细阐述了一下)。 –

+0

它的工作原理。非常感谢:) –

您需要name属性添加到您的输入字段,例如:

<input id="countryText" type="text" > 

应该改为:

<input id="countryText" name="countryText" type="text" > 

另外,还要确保你的字段是表单中。

request.getParameter取input元素的name属性。在这里你已经给出了ID。 尝试为您的输入元素添加名称属性。

<table> 
       <tr> 
        <td>Country</td> 
        <td><input name="countryText" type="text" > </td> 
       </tr> 
       <tr> 
        <td>City</td> 
        <td><input name="cityText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>Check in Date</td> 
        <td><input name="checkinText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>No of Nights</td> 
        <td><input name="noOfNightsText" type="text" ></td> 
       </tr> 
       <tr> 
        <td>No of Rooms</td> 
        <td><input name="noOfRoomsText" value="1" type="text" disabled></td> 
       </tr> 
       <tr> 
        <td></td> 
        <td align="right"><button id="searchButton" type="submit" class="buttonClass">Search Hotels</button></td> 
       </tr> 
      </table> 

request.getParameter期望名称不是id来获取字段的值。

所以使用name属性,并通过内部的名字你request.getParameter(name)

这将解决你的问题100%

一个快速的解决方案

把你的ID和名称相同。无需更改任何显示内容

+0

嗨@KB,同时回答哈哈... !! :) –

+0

嗨Ramesh..ya什么巧合亲爱的... –