Character encoding issue with Tomcat

OakvilleWork picture OakvilleWork · Jun 18, 2012 · Viewed 54.3k times · Source

There is strange character encoding going on. I am using JSP (JSTL) and Struts with Tomat 6.

I have my JSP page encoding as such:

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

The issue is when I try to pass the url using encodeURI as such:

<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:

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??

Answer

Rahul Agrawal picture Rahul Agrawal · Jun 25, 2012

Do following

1) HTML Code

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

2) Browser Setting for IE View -- Encoding -- Unicode (UTF-8)

3) Tomcat Server server.xml - In Connector tag added "URIEncoding" attribute as

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

catalina.sh/catalina.bat - added following

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"