设为首页 收藏本站
查看: 610|回复: 0

[经验分享] Character Encoding tomcat.

[复制链接]

尚未签到

发表于 2015-8-11 15:58:04 | 显示全部楼层 |阅读模式
  default character encoding of the request or response body:
    If a character encoding is not specified, the Servlet specification requires that an encoding of ISO-8859-1 is used.
  For JSP pages,The request character encoding handling is the same,for JSP pages in standard syntax the default response charset is the usual ISO-8859-1, but for the ones in XML syntax it is UTF-8.
  
  Default encoding for GET
    Many browsers are starting to offer (default) options of encoding URIs using UTF-8 instead of ISO-8859-1.
    HTML 4.0 recommends the use of UTF-8 to encode the query string.
  Default Encoding for POST
    ISO-8859-1 is defined as the default character set for HTTP request and response bodies in the servlet specification
  change how GET parameters are interpreted
  Tomcat will use ISO-8859-1 as the default character encoding of the entire URL, including the query string ("GET parameters").
    There are two ways to specify how GET parameters are interpreted:
      Set the URIEncoding attribute on the <Connector> element in server.xml to something specific (e.g. URIEncoding="UTF-8").
      Set the useBodyEncodingForURI attribute on the <Connector> element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.
  change how POST parameters are interpreted
    POST requests should specify the encoding of the parameters and values they send. Since many clients fail to set an explicit encoding, the default is used (ISO-8859-1).
  In many cases this is not the preferred interpretation so one can employ a javax.servlet.Filter to set request encodings. Writing such a filter is trivial. Furthermore Tomcat already comes with such an example filter. Please take a look at:
  5.x



1 webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
2 webapps/jsp-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
  6.x



1 webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.java
  5.5.36+, 6.0.36+, 7.x
  Since 7.0.20 the filter became first-class citizen and was moved from the examples into core Tomcat and is available to any web application without the need to compile and bundle it separately. See documentation for the list of filters provided by Tomcat. The class name is:



1 org.apache.catalina.filters.SetCharacterEncodingFilter
  Note:
    The request encoding setting is effective only if it is done earlier than parameters are parsed. Once parsing happens, there is no way back.
    Parameters parsing is triggered by the first method that asks for parameter name or value.
    Make sure that the filter is positioned before any other filters that ask for request parameters.
    The positioning depends on the order of filter-mapping declarations in the WEB-INF/web.xml file, though since Servlet 3.0 specification there are additional options to control the order.
    To check the actual order you can throw an Exception from your page and check its stack trace for filter names.
  What can you recommend to just make everything work? (How to use UTF-8 everywhere)
  Using UTF-8 as your character encoding for everything is a safe bet. This should work for pretty much every situation.
  In order to completely switch to using UTF-8, you need to make the following changes:




    • Set URIEncoding="UTF-8" on your <Connector> in server.xml. References: HTTP Connector, AJP Connector.


    • Use a character encoding filter with the default encoding set to UTF-8

    • Change all your JSPs to include charset name in their contentType.
      For example, use <%@page contentType="text/html; charset=UTF-8" %> for the usual JSP pages and <jsp:directive.page contentType="text/html; charset=UTF-8" /> for the pages in XML syntax (aka JSP Documents).

    • Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8.
      Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8").

    • Change any content-generation libraries you use (Velocity, Freemarker, etc.) to use UTF-8 and to specify UTF-8 in the content type of the responses that they generate.

    • Disable any valves or filters that may read request parameters before your character encoding filter or jsp page has a chance to set the encoding to UTF-8. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.


  How can I test if my configuration will work correctly?
    The following sample JSP should work on a clean Tomcat install for any input. If you set the URIEncoding="UTF-8" on the connector, it will also work with method="GET".



<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Character encoding test page</title>
</head>
<body>
<p>Data posted to this form was:
<%
request.setCharacterEncoding("UTF-8");
out.print(request.getParameter("mydata"));
%>
</p>
<form method="POST" action="index.jsp">
<input type="text" name="mydata">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</body>
</html>
  How can I send higher characters in my HTTP headers?
    You have to encode them in some way before you insert them into a header. Using url-encoding (% + high byte number + low byte number) would be a good idea.
  ref:  http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-97590-1-1.html 上篇帖子: tomcat生产环境优化 下篇帖子: eclipse+maven+tomcat构建web工程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表