`
no_123
  • 浏览: 2042 次
  • 性别: Icon_minigender_2
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Best practices for encoding issue

 
阅读更多
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"));
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics