检查使用JAVA(a jsp)选择哪些复选框 [英] checking which check boxes are selected using JAVA ( a jsp)

查看:78
本文介绍了检查使用JAVA(a jsp)选择哪些复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个显示带有复选框的简单表单的servlet,当用户选择他想要的复选框的数量并单击确认时,我的servlet中的POST请求会检查哪些框已经过检查和查询基于数据库。

我不确定如何在Java中执行此操作,因为用户可能选择1个或多个复选框。如果有人可以用一个小例子来解释这一点,那就太好了。



我对编程非常陌生,如果我知道如何去做,它会提供一个代码片段。 / p>

解决方案

这实际上是HTML表单行为问题。当您使用一个名称属性和不同的值属性检查几个复选框并按下提交按钮时,浏览器将通过选中的复选框值向服务器发送请求。所以你可以从这个url参数中获得值名称。



例如:

 < form name =inputaction =html_form_action.aspmethod =get> 
< input type =checkboxname =vehiclevalue =Bike>我有一辆自行车< br>
< input type =checkboxname =vehiclevalue =Car>我有一辆车
< br>< br>
< input type =submitvalue =提交>
< / form>

如果您同时选中两个复选框,您的服务器将收到如下参数:

  http://example.com/your_page.jsp?vehicle=Bike&vehicle=Car 

之后,您可以获得如下值:

 字符串checkboxValues = request.getParameter(vehicle); 

checkboxValues获取以逗号分隔的所有值。


I am trying to create a servlet that displays a simple form with checkboxes , when the user selects the number of checkboxes he wants and clicks on a "confirm" the POST request in my servlet checks for which boxes have been checked and queries the database based .

I am unsure on how to do this in Java as the user may select either 1 or more checkboxes . if somebody could explain this with a small example this would be great.

I am very new to programming and would provide a code snippet if I knew how to do it .

解决方案

This is actually the HTML form behavior question. When you check a few checkboxes with one "name" attribute and different "value" attributes and press submit button, your browser will send request to the server with checked checkbox values. So you can get value names from this url parameters.

For example:

<form name="input" action="html_form_action.asp" method="get">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car 
<br><br>
<input type="submit" value="Submit">
</form>

If you check both of checkboxes your server will receive this parameters like so:

http://example.com/your_page.jsp?vehicle=Bike&vehicle=Car 

After that you can get values like this:

String checkboxValues = request.getParameter("vehicle");

checkboxValues gets all values separated by comma.

这篇关于检查使用JAVA(a jsp)选择哪些复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆