Request获取请求参数中文乱码问题

  • 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);
    }

未添加的结果:?????
添加后的结果:张三

猜你喜欢

转载自blog.csdn.net/xuebanub1/article/details/129950989