JSP中getparametervalues获得复选框选中的值

来源:互联网 发布:淘宝卖家不给发票投诉 编辑:程序博客网 时间:2024/06/02 07:30
<body>
<!-- jsp页面中代码执行顺序:java>jsp>html>js
    1.java代码中必须加if(food!=null)否则会报空指针异常
    2.
 -->
<p>这里是食品柜台,请选择您要购买的食品:
<form action="" method=post name=form>
<input type="checkbox" name="choice" value="香肠">香肠
<input type="checkbox" name="choice" value="热狗">热狗
<input type="checkbox" name="choice" value="烤鸭">烤鸭
<input type="checkbox" name="choice" value="酸奶">酸奶
<br>
<input type="submit" value="提交" name="submit">

</form>


上面的代码中有好几个input都是name为choice,如果用request.getParameter("langtype")来取这些值,到底要取哪一个?所以在这个时候,不能用getParameter来取,而应该用getParameterValues来取,用这个方法,会将前端同名input type以阵列的方式取回,所以可以宣告一个String的阵列来承接,用法如下:


<%
//获得所有name=choice的标签中被选中的
String food[]=request.getParameterValues("choice");
if(food!=null)
{
    for(int a=0;a<food.length;a++)
    {
        out.print(food[a]);
    }
}
%>
</body>
原创粉丝点击