<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Form表单元素</title>
</head>
<body>
<form name="form1" method="post" action="getallpr.jsp">
<table width="1000" align="center" cellpadding="5" cellspacing="1">
<tr><td width="200">用户名:<input name="textfield" type="text" id="textfield" value="" size="10"></td></tr>
<tr><td width="200">密 码:<input name="psword" type="password" id="psword" value="" size="10"></td></tr>
<tr><td colspan="2">上传头像:<input type="file" name="importfile" id="importfile"></td></tr>
<tr><td>喜欢的运动:
<input type="checkbox" name="checkbox1" id="checkbox1" value="篮球">篮球
<input type="checkbox" name="checkbox2" id="checkbox2" value="足球">足球
<input type="checkbox" name="checkbox3" id="checkbox3" value="乒乓球">乒乓球
<input type="checkbox" name="checkbox4" id="checkbox4" value="dota">dota
<input type="checkbox" name="checkbox5" id="checkbox5" value="台球">台球
</td></tr>
<tr><td>最喜欢的球队:
<input type="radio" name="radio" id="radio1" value="巴萨">巴萨
<input type="radio" name="radio" id="radio2" value="皇马">皇马
<input type="radio" name="radio" id="radio3" value="拜仁">拜仁
<input type="radio" name="radio" id="radio4" value="曼联">曼联
<input type="radio" name="radio" id="radio5" value="米兰">米兰
</td></tr>
<tr><td>最喜欢的球队:
<select name="select" size="1" id="select">
<option>巴萨</option>
<option>皇马</option>
<option>拜仁</option>
<option>曼联</option>
<option>米兰</option>
</select>
</td></tr>
<tr><td><input type="submit" name="button" id="button" value="提交" >
<input type="reset" name="button2" id="button2" value="重置">
</td></tr>
</table>
</form>
</body>
</html>
<!-- getallpr.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'getallpr.jsp' starting page</title>
<% request.setCharacterEncoding("utf-8") ; %>
</head>
<body>
<%
java.util.Enumeration allPara= request.getParameterNames();
while(allPara.hasMoreElements()){
String pname=(String)allPara.nextElement(); //取得各个元素的name值
String rs=request.getParameter(pname); //通过元素的name值取得value
%>
第<%=pname %>个元素的值为:<%=rs %><br>
<%
}
%>
</body>
</html>
request.getParameterNames()方法request.getParameterNames()方法是将发送请求页面中form表单里所有具有name属性的表单对象获取(包括button).返回一个Enumeration类型的枚举.
通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值.
最后通过request.getParameter()方法获取表单控件的value值.