Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从jsp到Servlet的下拉菜单中捕获值?_Jsp_Drop Down Menu_Web Applications - Fatal编程技术网

如何从jsp到Servlet的下拉菜单中捕获值?

如何从jsp到Servlet的下拉菜单中捕获值?,jsp,drop-down-menu,web-applications,Jsp,Drop Down Menu,Web Applications,我想在JSP上使用下拉菜单,但是。。。我不知道如何捕获所选项目的值并将其传递给我的Servlet,并进行一些查询以将值添加到我的数据库中 你能给我一些如何编码的想法或线索吗 另外,我还需要将下拉菜单中的项目转换为整数,因为我将把它添加到数据库中存储的数据中 对于像我这样的先发球员来说这会很难吗? 我应该使用文本框,让用户手动输入整数而不是下拉菜单吗 提前多谢:) 我的Jsp菜单如下所示: <body> <form action="AddPoints">

我想在JSP上使用下拉菜单,但是。。。我不知道如何捕获所选项目的值并将其传递给我的Servlet,并进行一些查询以将值添加到我的数据库中

你能给我一些如何编码的想法或线索吗

另外,我还需要将下拉菜单中的项目转换为整数,因为我将把它添加到数据库中存储的数据中

对于像我这样的先发球员来说这会很难吗? 我应该使用文本框,让用户手动输入整数而不是下拉菜单吗

提前多谢:)

我的Jsp菜单如下所示:

<body>
    <form action="AddPoints">
      <table width="408" border="0">
        <tr>
          <td width="402"><h3 align="center">Enter Points:</h3>
            <h3 align="center">
              <label for="Points"></label>
              <select name="Points" size="1" id="Points">
                <option value="5" selected>5</option>
                <option value="10">10</option>
                <option value="15">15</option>
                <option value="20">20</option>
                <option value="25">25</option>
              </select>
              <br/>
            </h3>
            <h3 align="center"><strong></strong>
              <input type="submit" name="AddPoints" id="AddPoints" value="Add Points">
          </h3></td>
        </tr>
      </table>
</form>
</body>
 var selection = request.getParameter('Points');

输入点:
5.
10
15
20
25

我还想知道这一行的值是否是我的servlet能够捕获的真正值:
25


对不起,如果我有这么多问题…:)

首先,您可能希望在表单标记中添加一个Method='post',以便将数据传递到jsp

至于实际检索所选值,您的代码可能希望如下所示:

<body>
    <form action="AddPoints">
      <table width="408" border="0">
        <tr>
          <td width="402"><h3 align="center">Enter Points:</h3>
            <h3 align="center">
              <label for="Points"></label>
              <select name="Points" size="1" id="Points">
                <option value="5" selected>5</option>
                <option value="10">10</option>
                <option value="15">15</option>
                <option value="20">20</option>
                <option value="25">25</option>
              </select>
              <br/>
            </h3>
            <h3 align="center"><strong></strong>
              <input type="submit" name="AddPoints" id="AddPoints" value="Add Points">
          </h3></td>
        </tr>
      </table>
</form>
</body>
 var selection = request.getParameter('Points');
然后将所选值存储在一个变量中,您可以在SQL查询中使用该变量

比如:

var sQL = "Select * From xxx where Points="+selection
确保您有一个整数可以在jsp中使用方便的parseInt()函数完成


关于你的最后一个问题。值属性是实际捕获的值。是的,选项标记之间的数字就是实际显示给用户的值。

错误代码。当参数为
null
时,将引发
NumberFormatException
。即使如此,理论上它也会在自动装箱时抛出一个
NullPointerException
,而不是
!=null
因为
int
永远不能
null
。更好的设计是在服务器端维护选项值及其标签,如在
ServletContextListener
或其他类型中。检查[
int selectedItem;

if((selectedItem=Integer.ParseInt(request.getParameter("Points"))!=null)
{

         // It woud take Less Time
         // Do Your Logic
}