Tomcat 中文乱码 设置UTF-8编码 问题解决办法
在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编码 问题解决办法的更多相关文章
- xshell实时跟踪日志与中文乱码设置
1.实时跟踪日志命令 tail -f logName.log 动态查看名为logName的日志信息 ctrl+c 退出实时跟踪 2.中文乱码设置 在Xshell.putty.SSH Secure Sh ...
- [转] Filezilla server设置指南及中文乱码、登录欢迎语问题解决
一.filezilla server 安装指南:FileZilla是一款免费而且开源的FTP工具.包括FileZilla Client,FileZilla Server两个版本.FileZilla S ...
- get请求中文乱码及get,post编码探究
在我使用get请求进行查询的时候遇到一个问题: 当我的请求参数中有中文时,出现乱码. 可是即使我设置了Spring的characterEncodingFilter,也还是出现乱码. 原因:tomcat ...
- Tomcat中文乱码问题
新从官网下载的Tomcat7和Tomcat8,在运行的时候都会有乱码的问题,就此发现问题,我们就给它就地正法! 经过初步的分析,问题产生的大概原因是由于Tomcat的log日志模块不识别中文的问题, ...
- Tomcat 中文乱码
问题描述 tomcat9启动后会有中文乱码,比如控制台乱码: startup.bat启动时乱码: 解决方法 打开"/apache-tomcat-9.0.20/conf/logging.pro ...
- 采用DoGet方式提交中文,乱码产生原因分析及解决办法
前段时间某功能在测试机器上出现乱码,情况如下: 现象: 调试搜索功能时,通过doGet方法提交到后台的中文参数在本地和开发测试机器上为乱码(Action层),在测试人员测试机器 ...
- IDEA2018.3.5Tomcat output 中文乱码 修改配置文件生效的解决办法
首先,我也是尝试别人介绍的方法: IDEA Windows 环境 console 乱码问题 - intellij idea 15 控制台输出中文乱码问题解决办法 - liuhai的博客 - CSDN博 ...
- Tomcat 中文乱码,设置UTF-8
1.修改tomcat的conf目录下 server.xml文件加上 URIEncoding="UTF-8" <Connector port="8080" ...
- 聊聊、Tomcat中文乱码和JVM设置
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx512m -Dfile.encoding=GBK -Dsun.jnu.encoding=GBK
随机推荐
- 《Bilateral Multi-Perspective Matching for Natural Language Sentences》(句子匹配)
问题: Natural language sentence matching (NLSM),自然语言句子匹配,是指比较两个句子并判断句子间关系,是许多任务的一项基本技术.针对NLSM任务,目前有两种流 ...
- python xml练习:从database.xml文件取databaselist的ip、name、passwd,写入列表
xml: <?xml version='1.1' encoding='utf-8'?><!--this is a test about xml--><databaseli ...
- web前端----Bootstrap框架补充
一.一个小知识点 1.截取长屏的操作 2.设置默认格式 3.md,sm, xs 4.空格和没有空格的选择器 二.响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等- 响应式怎 ...
- Finalize和Dispose的区别
https://www.cnblogs.com/Jessy/articles/2552839.html
- MySQL Crash Course #19# Chapter 27. Globalization and Localization
Globalization and Localization When discussing multiple languages and characters sets, you will run ...
- mysql 触发器 trigger用法 three (稍微复杂的)
MySQL包含对触发器的支持.触发器是一种与表操作有关的数据库对象,当触发器所在表上出现指定事件时,将调用该对象,即表的操作事件触发表上的触发器的执行. 创建触发器 在MySQL中,创建触发器语法如下 ...
- Python3.x(windows系统)安装libxml2库
Python3.x(windows系统)安装libxml2库 cmd安装命令: pip install lxml 执行结果: 再执行命令: pip install virtualenv 执行结果:
- AJAX 与 Python 后台通信
Ajax 简介 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技术. Ajax = 异步 ...
- 20145330 《网络对抗》 Web安全基础实践
20145330 <网络对抗> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字 ...
- STM32.printf
printf("\r\n this is a usart printf demo \r\n"); Use Micro LIB 需要勾选这个库 将串口定义成 printf 函数 #i ...