对于客户端发来的汉字,我们一般需要转码:

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

request.setCharacterEncoding("UTF-8");//这样设置客户机发来数据文字格式只对post方式有效

String line = request.getParameter("username");

System.out.println(line);

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

String line = request.getParameter("username");

line=(new String(line.getBytes("iso8859-1"),"UTF-8"));//对于get方式,只能这样了。

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

有没有办法不用这么麻烦?有,配置Tomcat服务器的server.xml 中的connector

先看一下API:http://localhost:8080/docs/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.

意思是如果不配置这个URIEncoding 那么采用默认的ISO-8859-1

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

这样配置即可。

还有一个方法:

useBodyEncodingForURI

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" useBodyEncodingForURI="true"/>

这样一改,那么:

request.setCharacterEncoding("UTF-8");//这样设置客户机发来数据文字格式就不只对post方式有效了,对get方式也有效。

Tomcat 的log乱码解决方式:

catalina.bat 中找到这一句

set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"      在后边加上:-Dfile.encoding="UTF-8"

最新文章

  1. 手机支持USB功能、驱动文件对应关系
  2. Rails problem
  3. z-index、display、selector选择器优先级css优先级面试用到
  4. PopupWindow
  5. 从问题看本质: 研究TCP close_wait的内幕
  6. PYTHON不定参数与__DOC__
  7. ctkPlugin插件系统实现项目插件式开发
  8. Android WindowManager的使用
  9. CentOS修改系统默认语言与编码
  10. [转]-bash: wget: command not found的两种解决方法
  11. poj1236 有向图加边变成强连通图
  12. LCS最长公共子序列~dp学习~4
  13. ViewPager适配器学习记录( pageAdapter和FragmentPagerAdapter/FragmentStatePagerAdapter))
  14. JAVA基础经典面试
  15. 第四节:框架前期准备篇之进程外Session的两种配置方式
  16. greenev —— Python 异步网络服务框架
  17. Android开发之Activity(cho1)篇
  18. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十六:IIC储存模块
  19. fiddler的inspectors传入的参数乱码
  20. Python之打印变量

热门文章

  1. apose 根据excel 导出模版
  2. [转]软件版本号扫盲——Beta RC Preview release等
  3. MongoDB基础学习(一) MongoDB概念解析
  4. linux基础命令学习 (十)Vi
  5. 给WebAPI的REST接口添加测试页面(三)
  6. [转]远程注入DLL : 取得句柄的令牌 OpenProcessToken()
  7. animate()--jq动画
  8. cocos2d-x_lua中tolua++绑定c++分享
  9. 宿主机訪问virtualBox中Ubuntu
  10. LoadLibrary文件路径及windows API相关的文件路径问题