@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决中文乱码问题
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String hobby = req.getParameter("hobby");
// 有多个值的话要使用这个方法
String[] hobby2 = req.getParameterValues("hobby");
System.out.println("username=>"+username);
System.out.println("password=>"+password);
System.out.println("hobby=>"+hobby);
System.out.println("hobby=>"+ Arrays.toString(hobby2));
}
Title
当我们的请求是post请求的时候,如果表单中有中文的话,会出现中文乱码的问题
当然解决这个问题的方式很简单,就是修改一下字符编码集
req.setCharacterEncoding("UTF-8");
注意!!!!!!
req.setCharacterEncoding("UTF-8"); 此语句一定要在获取请求的参数之前使用才可以实现效果
像下面的代码一样,还是会出现乱码问题!!!!
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
// 解决中文乱码问题
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String password = req.getParameter("password");
// 有多个值的话要使用这个方法
String[] hobby = req.getParameterValues("hobby");
System.out.println("username=>"+username);
System.out.println("password=>"+password);
System.out.println("hobby=>"+ Arrays.toString(hobby));
}



