如何从 jsp 的 DropDown 菜单中捕获值到 Servlet?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您可能希望将 Method='post' 添加到表单标记,以便将数据传递到 jsp。
至于实际检索所选值,您的代码可能希望如下所示:
然后,您将所选值存储在可在 SQL 查询中使用的变量中。
像这样的事情:
确保您有一个整数可以在 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:
You then have the selected value stashed in a variable that you can use in an SQL query.
Something like:
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