简体   繁体   English

Tomcat的字符编码问题

[英]Character encoding issue with Tomcat

There is strange character encoding going on. 正在进行奇怪的字符编码。 I am using JSP (JSTL) and Struts with Tomat 6. 我正在使用JSP(JSTL)和Struts与Tomat 6。

I have my JSP page encoding as such: 我有我的JSP页面编码:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

The issue is when I try to pass the url using encodeURI as such: 问题是当我尝试使用encodeURI传递url时:

<script type="text/javascript">
          $('#mailer_filter').change(function(){
            var val = $(this).val();
            console.log(val);
            console.log(escape(val));
            console.log(encodeURI(val));
            location.href = 'mailList.a?' + encodeURI($(this).val());
          });
        </script>

the parameter on the action (java end) comes out as: action(java end)上的参数如下:

Gaz Métro

however on the front end it is displayed as: 但是在前端它显示为:

Gaz Métro

which is the correct way. 这是正确的方法。 What I can do about this?? 我能做些什么?

Do following 做以下

1) HTML Code 1)HTML代码

 <meta contentType="text/html; charset="UTF-8"/>

2) Browser Setting for IE View -- Encoding -- Unicode (UTF-8) 2)IE视图的浏览器设置 - 编码 - Unicode(UTF-8)

3) Tomcat Server server.xml - In Connector tag added "URIEncoding" attribute as 3)Tomcat Server server.xml - 在Connector标签中添加“URIEncoding”属性为

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

catalina.sh/catalina.bat - added following catalina.sh/catalina.bat - 添加以下内容

set JAVA_OPTS=--Xms256m -Xmx1024m -Xss268k -server -XX:MaxPermSize=256m -XX:-UseGCOverheadLimit -Djava.awt.headless=true -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

set CATALINA_OPTS=-Dfile.encoding="UTF-8"

4) MIME type of response should be "application/x-www-form-urlencoded" 4)MIME类型的响应应该是“application / x-www-form-urlencoded”

Have you followed these steps? 你有没有遵循这些步骤?

http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8

Copied below: 复制如下:

Using UTF-8 as your character encoding for everything is a safe bet. 使用UTF-8作为一切的字符编码是一个安全的选择。 This should work for pretty much every situation. 这应该适用于几乎所有情况。

In order to completely switch to using UTF-8, you need to make the following changes: 要完全切换到使用UTF-8,您需要进行以下更改:

  1. Set URIEncoding="UTF-8" on your in server.xml. 在server.xml中设置URIEncoding =“UTF-8”。 References: HTTP Connector, AJP Connector. 参考:HTTP连接器,AJP连接器。

  2. Use a character encoding filter with the default encoding set to UTF-8 使用字符编码筛选器,默认编码设置为UTF-8

  3. Change all your JSPs to include charset name in their contentType. 更改所有JSP以在其contentType中包含charset名称。

    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). 例如,对于通常的JSP页面使用<%@page contentType="text/html; charset=UTF-8" %> ,并使用<jsp:directive.page contentType="text/html; charset=UTF-8" />对于XML语法中的页面(又名JSP文档)。

  4. Change all your servlets to set the content type for responses and to include charset name in the content type to be UTF-8. 更改所有servlet以设置响应的内容类型,并在内容类型中包含charset name为UTF-8。

    Use response.setContentType("text/html; charset=UTF-8") or response.setCharacterEncoding("UTF-8") . 使用response.setContentType("text/html; charset=UTF-8")response.setCharacterEncoding("UTF-8")

  5. 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. 更改您使用的任何内容生成库(Velocity,Freemarker等)以使用UTF-8并在其生成的响应的内容类型中指定UTF-8。

  6. 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. 在字符编码过滤器或jsp页面有机会将编码设置为UTF-8之前,禁用可能读取请求参数的任何阀门或过滤器。 For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html . 有关更多信息,请访问http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html

Try setting the URIEncoding parameter of your tomcat connector (in the server.xml) to UTF-8: 尝试将tomcat连接器的URIEncoding参数(在server.xml中)设置为UTF-8:

Eg: 例如:

<Connector port="8080" maxHttpHeaderSize="8192"
           maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           connectionTimeout="20000" disableUploadTimeout="true"
           URIEncoding="UTF-8"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM