YuFeng

Get请求 和 Post请求
Get请求是将表单内容以URL的形式发送... 默认的编码格式 iso-8859-1 解决乱码的方式: p...
扫描右侧二维码阅读全文
25
2019/06

Get请求 和 Post请求

Get请求是将表单内容以URL的形式发送...  默认的编码格式    iso-8859-1
解决乱码的方式:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取form表单的内容...  get方式
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
        for(Map.Entry<String, String[]> entry : entrySet) {
            System.out.print(entry.getKey());
            System.out.println("---->");
            for(String s : entry.getValue()) {
                System.out.print(new String(s.getBytes("ISO-8859-1"),"utf-8"));
                System.out.println(" ");
            }
            System.out.println("\r");
        }
    }


Post请求默认的编码格式也是  iso-8859-1   但是可以通过下面方法解决...将页面内容以UTF-8来解码    
据猜测:当网站post内容之前先发送请求,询问此次提交是以什么编码发送,于是有了下面这行代码
request.setCharacterEncoding("utf-8");  
解决乱码的方式:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取form表单内容 post方式
        System.out.println("post执行了");
        //request.setCharacterEncoding("utf-8");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
        for(Entry<String, String[]> e : entrySet) {
            System.out.print(e.getKey());
            System.out.println();
            for(String s : e.getValue()) {
                System.out.print(s);
            }
            System.out.println("------");
        }
    }
Last modification:June 25th, 2019 at 09:11 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment