Tomcat 改服务器编码(Java 修改字符串编码格式)

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

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

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" 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值