Servlet处理get请求时的中文乱码问题 您所在的位置:网站首页 get中文乱码解决 Servlet处理get请求时的中文乱码问题

Servlet处理get请求时的中文乱码问题

2023-03-21 08:12| 来源: 网络整理| 查看: 265

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

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

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

1234567891011121314 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

我们可能改动过这里的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上面来,则也需要特殊注意这个问题,可能要删减代码中响应乱码的处理模块了。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有