
相关资料:
《21天学通Java Web开发》
获得参数的所有参数值(多个值)
1.需要使用request对象的getParameterValues()方法。
RequestForm4.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>表单</title>
</head>
<body>
<form action="RequestDemo4.jsp" method="post">
用户名:<input type= "text" name="username"/><br>
用户密码:<input type= "password" name="userpassword"/><br>
喜欢的运动:
<input type = "checkbox" name="sport" value="pingpang">乒乓球
<input type = "checkbox" name="sport" value="badketball">篮球
<input type = "checkbox" name="sport" value="football">足球<br>
<input type="submit" value="提交">
</form>
</body>
</html>
RequestDemo4.jsp
<%@ page language="java" contentType="text/html;charset=gb2312" import="java.util.*" %>
<html>
<head>
<title>使用request对象获得参数的所有参数值</title>
</head>
<body>
<%-- 通过request对象的getParameterValues获得参数的所有参数值 --%>
<%
String[] strArr = request.getParameterValues("sport");//获得所有的参数值
out.println("喜欢的运动:");//输出信息
for(String str: strArr){ //遍历字符串数组取出参数值
out.println(str); //输出参数值
}
%>
</body>
</html>