- get请求不会乱码,因为Tomcat8以上修复了中文乱码问题
- post请求会乱码,解决方法:在获取参数前,设置request的编码
request.setCharacterEncoding("UTF-8");
发送post请求,请求参数 username:张三
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//新添加的
String username = request.getParameter("username");
System.out.println(username);
}
未添加的结果:?????
添加后的结果:张三