javaWeb request乱码处理

时间:2022-11-02 18:51:16

//解决get方式提交的乱码
        String name = request.getParameter("name");
        name=new String(username.getBytes("iso8859-1"),"utf-8");

//解决post方式提交的乱码
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");

//最终方案

   response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        if (name.equals(new String(name.getBytes("iso8859-1"), "iso8859-1"))) {
            name = new String(request.getParameter("name")
                    .getBytes("iso8859-1"), "utf-8");
        }