zoukankan      html  css  js  c++  java
  • get请求 解决乱码问题

    我们都知道,使用Servlet处理get请求时,如果get请求的参数中有中文,直接接收会是乱码,这个时候我们使用类似下面的语句来处理乱码:

    1
    2
    3
    4
    5
    String name = request.getParameter("name");
    System.out.prinlnt(name); // 乱码
    // 处理乱码
    name = new String(name.getBytes("ISO8859-1"),"UTF-8");
    System.out.println(name);// 乱码问题解决

    这时候每次中文都要处理,比较麻烦,我们可能会使用过滤器,使用类型下面的代码处理乱码问题:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public String getParameter(String name) {
    String value = super.getParameter(name);
    if (value == null)
    return null;
    String method = request.getMethod();
    if ("get".equalsIgnoreCase(method)) {
    try {
    value = new String(value.getBytes("ISO8859-1"),"UTF-8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    }
    return value;
    }

    但是,这是为什么呢?为什么我们需要将ISO8859-1转为UTF-8?为什么我们接收到的参数是ISO8859-1这种编码方式的?
    其实很简单,只是个配置问题:
    在tomcat安装目录下的conf/server.xml中,有如下的配置:

    1
    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

    我们可能改动过这里的port为9999,8888等自己喜欢的端口号,这里呢,也可以设置另外一个跟上述编码问题有关的参数信息:URIEncoding,该配置决定了使用get请求通过浏览器地址栏访问tomcat时的编码方式,默认的编码方式使ISO8859-1,这一点我们可以从官网文档https://tomcat.apache.org/tomcat-7.0-doc/config/http.html) 获悉:

    URIEncoding:This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

    知道了这点,接下来就简单了,我们可以这样配置,则上述代码中,就不需要再从ISO8859-1转为UTF-8了:

    1
    URIEncoding="UTF-8"

    这显然比上述两种方式简单多了。

    值得注意的是,从tomcat8.0开始,URIEncoding默认值不再是ISO8859-1,而变成了UTF-8 
    官方文档 (https://tomcat.apache.org/tomcat-8.0-doc/config/http.html) 中是这么说的:

    URIEncoding:This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, UTF-8 will be used unless theorg.apache.catalina.STRICT_SERVLET_COMPLIANCE system property is set to true in which case ISO-8859-1 will be used.

    那么也就意味着,从tomcat8.0开始,get请求中的中文参数,不需要特殊处理了。而如果是tomcat8之前的项目要迁移到tomcat8上面来,则也需要特殊注意这个问题,可能要删减代码中响应乱码的处理模块了。

  • 相关阅读:
    Flash 终将谢幕:微软将于年底( 2020 年 )停止对 Flash 的支持
    分布式id生成方案总结
    如何设计一个亿级网关(API Gateway)?
    服务之间的调用为啥不直接用 HTTP 而用 RPC?
    Dubbo 总结:关于 Dubbo 的重要知识点
    CAP理论解读
    单点登录(SSO)的设计与实现
    入职微软三个月把老板炒了,七个月自己跑路是一种怎样的体验?
    有关链表的小技巧,我都给你总结好了
    排序算法入门之「选择排序」
  • 原文地址:https://www.cnblogs.com/guagua-join-1/p/8799141.html
Copyright © 2011-2022 走看看