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

发布于 2024-10-22 01:54:22 字数 1428 浏览 2 评论 0原文

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

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

附言。我还需要将下拉菜单中的项目转换为整数,因为我会将其添加到数据库中存储的数据中。

对于我这样的初学者来说会很难吗? 我应该使用文本框并让用户手动输入整数而不是下拉菜单吗?

非常感谢:)

我的 Jsp Menu 是这样的:

<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>

我也想知道这一行的 value 是否: 是我的 servlet 可以捕获的真实值吗?

抱歉,如果我有这么多问题......:)

I want to use a dropdown menu on my JSP but... I have no idea how to capture the Value of the Selected Item and Pass it on to my Servlet and have some QUERY to add the value to my database.

Can you give me some idea or clue how to code it?

PS. I also need items at dropdown menu converted into an Integer cause i'll be adding it to the stored data at my database.

Will this be hard for a starter like me?
should I use Textbox and let the user input an INTEGER manually instead of a Dropdown menu?

Thanks a lot in advance :)

My Jsp Menu is like this:

<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>

Also I am wondering if the value at this line: <option value="25">25</option> is the real value that my servlet can capture?

Sorry if i have so many questions... :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-10-29 01:54:22
int selectedItem;
if(request.getParameter("Points")!=null)
{
   selectedItem=Integer.ParseInt(request.getParameter("Points"));
}
int selectedItem;
if(request.getParameter("Points")!=null)
{
   selectedItem=Integer.ParseInt(request.getParameter("Points"));
}
梦萦几度 2024-10-29 01:54:22

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

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

 var selection = request.getParameter('Points');

然后,您将所选值存储在可在 SQL 查询中使用的变量中。

像这样的事情:

var sQL = "Select * From xxx where Points="+selection

确保您有一个整数可以在 jsp 中使用方便的 parseInt() 函数来完成

至于您的最后一个问题。 value 属性是实际捕获的内容是的,选项标签之间的数字正是实际显示给用户的内容

First up you probably want to add a Method='post' to your form tag so that is passes data on to the jsp.

As for actually retrieving the selected value your code will probably want to look something like this:

 var selection = request.getParameter('Points');

You then have the selected value stashed in a variable that you can use in an SQL query.

Something like:

var sQL = "Select * From xxx where Points="+selection

Making sure that you have an integer can be accomplished in jsp with the handy parseInt() function

As to your last question. the value attribute is what will actually be caught yes, the number between the option tags is just what is actually displayed to the user

避讳 2024-10-29 01:54:22
int selectedItem;

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

         // It woud take Less Time
         // Do Your Logic
}
int selectedItem;

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

         // It woud take Less Time
         // Do Your Logic
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文