1.请求体的内容通常是通过post来提交的,格式是
username=zhansan&password=123&hobby=football||&hobby=basketball
key--------------------------------value
username [zhangsan]
password [123]
hobby [football,basketball]
2.定义和用法
// 1.获取单个表单中的value值
String usernam = request.getParameter("usernam");
String password = request.getParameter("password");
// 2.获取多个表单的value值
String[] parameterValues = request.getParameterValues("hobby");
System.out.print("hobby.values:");
// 3.获得所有参数的name
Enumeration<String> parameterNames = request.getParameterNames();
for (String string2 : parameterValues) {
System.out.println(parameterNames.nextElement()); } // 4.将获得的所有参数封装到一个Map<String,String[]>
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
System.out.println(entry.getKey());
for (String string : entry.getValue()) {
System.out.println(string);
}
System.out.println("----------------");
}
3.实例结果
4.完整代码
package com.hdh.requesd; import java.io.IOException;
import java.util.Enumeration;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ContentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取单个表单中的value值
String usernam = request.getParameter("usernam");
String password = request.getParameter("password");
System.out.println("usernam.value:" + usernam);
System.out.println("password.value:" + password);
// 2.获取多个表单的value值
String[] parameterValues = request.getParameterValues("hobby");
System.out.print("hobby.values:");
for (String string : parameterValues) {
System.err.print(string + " ");
}
System.out.println("---------------------");
// 3.获得所有参数的name
Enumeration<String> parameterNames = request.getParameterNames();
for (String string2 : parameterValues) {
System.out.println(parameterNames.nextElement()); }
System.out.println("---------------------");
// 4.将获得的所有参数封装到一个Map<String,String[]>中
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
System.out.println(entry.getKey());
for (String string : entry.getValue()) {
System.out.println(string);
}
System.out.println("----------------");
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<hr>
<form action="/WEB15/content" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="checkbox" name="hobby" value="zq">足球
<input type="checkbox" name="hobby" value="lq">篮球
<input type="checkbox" name="hobby" value="ppq">乒乓球
<input type="submit" value="提交">
</form>
</body>
</html>