程序实例:
ShowAllParameters.htm
<html>
<head>
<title>A sample show parameters</title>
</head>
<body>A sample show parameters
<form method="get" action="servlet/ShowParameters">
Item Number:<input type="text" name="itemNum"><br>
Quantity:<input type="text" name="quantity"><br>
Price each:<input type="text" name="price" value="$"><br>
First Name:<input type="text" name="firstName"><br>
Last Name:<input type="text" name="lastName"><br>
Middle Initial:<input type="text" name="initial"><br>
Shipping Address:<textarea name="address" row="3" cols="40"></textarea><br>
Credit Card:<br>
<!--单选框示例-->
<input type="radio" name="cardType" value="Visa">Visa<br>
<input type="radio" name="cardType" value="Master Card">Master Card<br>
<input type="radio" name="cardType" value="Amex">Amex<br>
<input type="radio" name="cardType" value="Discover">Discover<br>
<input type="radio" name="cardType" value="Java SmartCard">Java SmartCard<br>
<!--密码输入框-->
Credit Card password:<input type="password" name="cardNum"><br>
Repeat Credit Card password:<input type="password" name="cardNum"><br><br>
<center><input type="submit" value="Submit Order"></center>
</form>
</body>
</html>
ShowParameters.java
public class ShowParameters extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String title = "Reading All Request Parameters";
out.println("<html><head><title>读取所有参数</title></head>"
+ "<BODY BGCOLOR='#FDF5E6'>\n" + "<H1 ALIGN=CENTER>" + title
+ "</H1>\n" + "<table border=1 align=center>\n"
+ "<TR BGCOLOR=\"FFAD00\">\n"
+ "<TH>Parameter Name<TH>Parameter Value(s)");
// Returns an Enumeration of String objects containing the names of the parameters contained in this request.
Enumeration paramNames = request.getParameterNames();
// Tests if this enumeration contains more elements.
while(paramNames.hasMoreElements()) {
// Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
String paraName = (String)paramNames.nextElement();
out.println("<tr><td>" + paraName + "/n<td>");
// Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
// 注意参数paraName(变量)不能加双引号,否则就是找参数名叫paraName的对应值了。
String[] paramValues = request.getParameterValues(paraName);
// 这个参数只有一个值
if(paramValues.length == 1) {
String paramValue = paramValues[0];
if(paramValue.length() == 0) {
out.println("<I>no value</I>");
} else {
out.println(paramValue);
}
}else {
// 这个参数有好几条值
out.println("<UL>");
for(int i = 0; i < paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</table>/n<body><html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
doGet(request,response);
}
}
访问localhost:8080/testServlet/ShowAllParameters.htm
提交,获得request读取的全部数据