Best practices for encoding issue

1 篇文章 0 订阅
0 篇文章 0 订阅
1 Always use UTF-8 as encoding;
2 Try to set the encoding uniformly(in filter or web server side);
2.1 In tomcat6,
2.1.1 For get method, you may set encoding by adding URIEncoding cfg option in server.xml:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
2.1.2 For post method, you can only set encoding by request.setCharacterEncoding in your filter(Don't call any request.getParameter before request.setCharacterEncoding, or request.setCharacterEncoding will not take effect).
2.2 In weblogic 10.3, you may set encoding in 2 ways:
2.2.1 In filter, request.setCharacterEncoding will set the encoding for both post method and get method;
2.2.2 In weblogic.xml, the following setting will set the encoding for both post method and get method:
<wls:charset-params>
<wls:input-charset>
<wls:resource-path>/cvsearch/*</wls:resource-path>
<wls:java-charset-name>UTF-8</wls:java-charset-name>
</wls:input-charset>
</wls:charset-params>
3 In servlet, set the encoding of response before sending result to client: response.setContentType("text/javascript; charset=UTF-8");
4 In jsp page, set the charset attribute of contentType to UTF-8: <%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Some more scenario for encoding:
----------------------------------
1) String.getBytes()
String s = "a";
s.getBytes("UTF-8");
----------------------------------
2) jstl core import
<c:import url="/ajax/topMover_CDS_getAxisSeries.action" charEncoding="UTF-8"/>
----------------------------------
3) InputStreamReader
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值