• Servlet处理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 the org.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上面来,则也需要特殊注意这个问题,可能要删减代码中响应乱码的处理模块了。

  • 相关阅读:
    转inux Shell编程入门
    转CentOS — MySQL备份 Shell 脚本
    JAVA4种线程池的使用
    http://cyber-dojo.org/
    tomcat内存大小设置
    rails的数据库查询方法
    Java 微信公众号上传永久素材的方法
    微信回复图文消息
    plsql解决64位解决办法
    Ruby中使用patch HTTP方法
  • 原文地址:https://www.cnblogs.com/hyl8218/p/5811769.html
Copyright © 2020-2023  润新知