当请求的参数有多个值时,使用getParameterValues()方法
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("==========doGet请求==============");
System.out.println("获取请求参数name:" + req.getParameter("username"));
System.out.println("获取请求参数password:" + req.getParameter("password"));
String[] hobbies = req.getParameterValues("hobby");
System.out.println("获取请求参数hobby:" + Arrays.asList(hobbies));
}
解决Post请求中文乱码问题
在Post请求中,可能会出现中文乱码问题
req. setCharacterEncoding(); 方法可以解决中文乱码问题,但需要在获取请求参数之前调用才有效
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("==========doPost请求==============");
//设置请求体的字符集为UTF-8,从而解决post 请求的中文乱码问题
//需要在获取请求参数之前调用才有效
req. setCharacterEncoding("UTF-8");
System.out.println("获取请求参数name:" + req.getParameter("username"));
System.out.println("获取请求参数password:" + req.getParameter("password"));
String[] hobbies = req.getParameterValues("hobby");
System.out.println("获取请求参数hobby:" + Arrays.asList(hobbies));
}



