标签:Map集合 post请求 more htm method element 足球 cte protected
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/register" method="post">
<input type="text" placeholder="用户名" name="username">
<input type="password" placeholder="密码" name="password">
<input type="checkbox" name="hobby" value="lq">篮球
<input type="checkbox" name="hobby" value="zq">足球
<input type="checkbox" name="hobby" value="ppq">乒乓球
<input type="checkbox" name="hobby" value="ymq">羽毛球
<input type="submit" value="注册">
</form>
</body>
</html>
java代码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
* @author shkstart
* @create 2021-01-12 22:19
*/
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 封装了对应的获取表单中的参数信息的api方法
// 如果请求中携带的有中文信息 需要设置编码字符集
// 从tomcat8开始优化编码字符集,针对get请求
// 对于post请求需要手动指定
req.setCharacterEncoding("UTF-8");
// 获取用户名
String username = req.getParameter("username");
System.out.println(username);
System.out.println("---------------------------------------------------");
// 获取所有的值封装到map集合中
Map<String, String[]> map = req.getParameterMap();
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
// 遍历set集合
while (iterator.hasNext()) {
String name = iterator.next();
String[] values = map.get(name);
System.out.println(name + "------" + Arrays.toString(values));
}
System.out.println("---------------------------------------------------");
// commons-beanUtils
Enumeration<String> names = req.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String[] values = req.getParameterValues(name);
System.out.println(name + "------" + Arrays.toString(values));
}
System.out.println("---------------------------------------------------");
String[] hobbies = req.getParameterValues("hobby");
System.out.println("hobby------" + Arrays.toString(hobbies));
}
}
标签:Map集合 post请求 more htm method element 足球 cte protected
原文地址:https://www.cnblogs.com/zk2020/p/14269718.html