在Java Web开发中,http请求带有中文字符的URI如果不处理容易出现乱码问题;这是因为Tomcat容器默认编码是iso-8859-1引起的,因此要避免出现乱码就要需要做相应的处理。解决办法如下:

一、在tomcat的 server.xml中设置

打开server.xml文件,对文件中设置如下:

在HTTP/1.1中增加URIEncoding="utf-8;

<Connector port="8098" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>

---------------------------------------------------------------------------

...

<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8098" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

...

...

另外,若是使用的其它的容器,只需确认容器的默认编码,做相应的设置即可。

二、配置servlet 可在服务程序中添加拦截器,当然也可用其它方式设置

示例:

public class CommonInterceptor implements Interceptor {
public void intercept(Invocation inv) {
Controller c=inv.getController();

try {
// 设置Request中汉字的编码,该方法只对post请求有效,对get请求无效;

// 对于get请求,应该在server.xml中指定:URIEncoding=utf-8;

c.getRequest().setCharacterEncoding("utf-8");

c.getResponse().setCharacterEncoding("utf-8");

inv.invoke();
} catch (Exception e) {
e.printStackTrace();
}

}

}

三、servlet ---- response.setCharacter和request.setCharacterEncoding详解

1、request.setCharacterEncoding():用来确保发往服务器的参数以汉字的编码来提取,设置从request中取得的值或从数据库中取出的值。

指定后可以通过request.getParameter()获取自己想要的字符串,如果没有提前指定,则会按照服务器端默认的“iso-8859-1”来进行编码;该方法只对post请求有效,对get请求无效;对于get请求,应该在server.xml中指定:URIEncoding=utf-8;

注意:在执行request.setCharacterEncoding()之前不能执行request.getParameter()方法;

原因:应该是在执行第一个getParameter()的时候,java将会按照编码分析所有的提交内容,而后续的getParameter()不再进行分析,所以setCharacterEncoding()无效。而对于GET方法提交表单是,提交的内容在URL中,一开始就已经按照编码分析提交内容,setCharacterEncoding()自然就无效。

2、response.setCharacterEncoding():设置HTTP 响应的编码,用于设置服务器给客户端的数据的编码

一般不会用这个方法来设置响应编码,

一般使用response.setContentType()方法来设置HTTP 响应的编码,同时指定了浏览器显示的编码;

因为他在执行该方法通知服务器端以指定编码进行编码后,会自动调用response.setCharacterEncoding()方法来通知浏览器以指定编码来解码;使用此方法要在response.getWriter()执行之前或response提交之前;

四、如果确实是要处理get请求

可在参数获取时作转码处理

String string = request.getParamers("");
String = new String(string.getBytes("ISO8859-1","utf-8"));

提示:如果已经在tomcat的 server.xml中设置了,可以不对servlet再作配置,但为了稳妥起见(避免忘记配置tomcat),最好也在代码中对servlet作下设置。

Tomcat 中文乱码 设置UTF-8编码 问题解决办法的更多相关文章

  1. xshell实时跟踪日志与中文乱码设置

    1.实时跟踪日志命令 tail -f logName.log 动态查看名为logName的日志信息 ctrl+c 退出实时跟踪 2.中文乱码设置 在Xshell.putty.SSH Secure Sh ...

  2. [转] Filezilla server设置指南及中文乱码、登录欢迎语问题解决

    一.filezilla server 安装指南:FileZilla是一款免费而且开源的FTP工具.包括FileZilla Client,FileZilla Server两个版本.FileZilla S ...

  3. get请求中文乱码及get,post编码探究

    在我使用get请求进行查询的时候遇到一个问题: 当我的请求参数中有中文时,出现乱码. 可是即使我设置了Spring的characterEncodingFilter,也还是出现乱码. 原因:tomcat ...

  4. Tomcat中文乱码问题

    新从官网下载的Tomcat7和Tomcat8,在运行的时候都会有乱码的问题,就此发现问题,我们就给它就地正法! 经过初步的分析,问题产生的大概原因是由于Tomcat的log日志模块不识别中文的问题, ...

  5. Tomcat 中文乱码

    问题描述 tomcat9启动后会有中文乱码,比如控制台乱码: startup.bat启动时乱码: 解决方法 打开"/apache-tomcat-9.0.20/conf/logging.pro ...

  6. 采用DoGet方式提交中文,乱码产生原因分析及解决办法

    前段时间某功能在测试机器上出现乱码,情况如下:   现象:           调试搜索功能时,通过doGet方法提交到后台的中文参数在本地和开发测试机器上为乱码(Action层),在测试人员测试机器 ...

  7. IDEA2018.3.5Tomcat output 中文乱码 修改配置文件生效的解决办法

    首先,我也是尝试别人介绍的方法: IDEA Windows 环境 console 乱码问题 - intellij idea 15 控制台输出中文乱码问题解决办法 - liuhai的博客 - CSDN博 ...

  8. Tomcat 中文乱码,设置UTF-8

    1.修改tomcat的conf目录下 server.xml文件加上 URIEncoding="UTF-8" <Connector port="8080" ...

  9. 聊聊、Tomcat中文乱码和JVM设置

    set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx512m -Dfile.encoding=GBK -Dsun.jnu.encoding=GBK

随机推荐

  1. yii2关联查询两组一对一

    public function getMember1(){        return $this->hasOne(Member::className(), ['wechat_id' => ...

  2. meta 如何写

    阻止手机号加下划线,可拨打:<meta name="format-detection" content="telephone=no" />  (io ...

  3. EditPlus 中文版停止更新

    经过一段时间的考虑,我决定了终止 EditPlus 的中文版翻译工作. 感谢各位网友在这么长时间内对本汉化项目的关注.

  4. JsonPath的使用

    语法: JsonPath 描述 $ 根节点 @ 当前节点 .or[] 子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标示,如数组下标 [,] 支持迭代器中做多选 [start:end ...

  5. Azkaban 入门

    需求 实际当中经常有这些场景:每天有一个大任务,这个大任务可以分成A,B,C,D四个小任务,A,B任务之间没有依赖关系,C任务依赖A,B任务的结 果,D任务依赖C任务的结果.一般的做法是,开两个终端同 ...

  6. 测试开发-web测试要点

    参数输入考虑 参数数值包含1个.多个.很多个.null.参数值前后包含空格的2种情况   数字类型:正数.负数.0.0.0.+0.0.-0.0.指数.对数.分数.小数.复数.科学计数法的测试,全角的数 ...

  7. SpringBoot之集成WebSocket

    websocket是什么不做介绍.开发环境:jdk1.8,win7_64旗舰版,idea   1.初始化一个springboot项目   2.加入websocket依赖 <!-- springb ...

  8. java多线程----拒绝策略

    本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略介绍 ...

  9. 火狐使用Ctrl新开窗口不生效

    使用window.open新开页面,火狐浏览器无法使用Ctrl新开窗口后页面停留在当前页面,兼容性问题,使用<a>或者<router-link>标签即可解决 --贡献者:毛毛

  10. Hadoop MapReduce执行过程实例分析

    1.MapReduce是如何执行任务的?2.Mapper任务是怎样的一个过程?3.Reduce是如何执行任务的?4.键值对是如何编号的?5.实例,如何计算没见最高气温? 分析MapReduce执行过程 ...