在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. 支持向量机(SVM)中的 SMO算法

    1. 前言 最近又重新复习了一遍支持向量机(SVM).其实个人感觉SVM整体可以分成三个部分: 1. SVM理论本身:包括最大间隔超平面(Maximum Margin Classifier),拉格朗日 ...

  2. win10如何设置自动睡眠时间(修改电源计划不好用的情况下)

    https://answers.microsoft.com/en-us/windows/forum/windows_10-power/windows-10-sleeping-when-set-not- ...

  3. 修改MySQL数据库中表和表中字段的编码方式的方法

    今天向MySQL数据库中的一张表添加含有中文的数据,可是老是出异常,检查程序并没有发现错误,无奈呀,后来重新检查这张表发现表的编码方式为latin1并且原想可以插入中文的字段的编码方式也是latin1 ...

  4. 基于webview的Hybrid app和React Native及html5

    基于webview的Hybrid app和React Native及html5 React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iO ...

  5. python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法

    python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...

  6. python如何序列化json数据

    使用json模块提供的loads方法和dumps方法,可以很方便的载入和读取json数据格式.而在具体实际应用中,我们使用python数据格式是 string.list 或dict等,这类格式如何直接 ...

  7. Linux学习笔记之Linux Centos关闭防火墙

    # Centos6.x /etc/init.d/iptables stop chkconfig iptables off sed -i 's/SELINUX=enforcing/SELINUX=dis ...

  8. python文件操作-r、w、a、r+、w+、a+和b模式

    对文件操作的基本步骤 f=open('a.txt','r',encoding='utf-8') data=f.read() print(data) f.close() 文件的打开和关闭使用open() ...

  9. html判断当前页面是否在iframe中/顶级document中

    在使用div+iframe布局的应用中,通常我们希望在session超时或者未登录访问时跳转到登录页面,默认情况下iframe中的页面无法直接覆盖父页面,因此需要在登录页面加载的时候判断一下当前是否为 ...

  10. Win10 Ubuntu 双系统 卸载 Ubuntu

    Win10 Ubuntu 双系统 卸载 Ubuntu 其实卸载 Ubuntu 系统很简单,进 win10 系统之后,磁盘管理,格式化 Ubuntu 的磁盘就可以了. 但是最费劲的是什么呢? 就是格式化 ...