18

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

Hash
  • 4,647
  • 5
  • 21
  • 39
OakvilleWork
  • 2,377
  • 5
  • 25
  • 37
  • 2
    Where do you see the value on the Java end? In the log file? If so, it may be that the editor that you're using to read the log file isn't expecting UTF-8... – atk Jun 18 '12 at 19:41

3 Answers3

21

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"

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • I've been searching for a day to get this sorted out. All worked fine on Jetty, but when deploying to tomcat my umlauts were going missing. Thank you! – Will Feb 03 '13 at 18:19
11

Have you followed these steps?

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

Copied below:

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:

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

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

  3. 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).

  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.

    Use response.setContentType("text/html; charset=UTF-8") or 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.

  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. For more information see http://www.mail-archive.com/users@tomcat.apache.org/msg21117.html.

Community
  • 1
  • 1
Paul Grime
  • 14,970
  • 4
  • 36
  • 58
  • thanks so much Paul... I've tried all of the above.. still no luck, i've been trying to find a solution, i've even seen solutions suggested to encode to UTF-8 manually! I do not want to use this option though... basically this post is similar to my problem: http://stackoverflow.com/questions/6213377/handle-french-characters-in-java however those suggestions don't work either. – OakvilleWork Jun 18 '12 at 21:35
  • I've seen **6** responsible for this 'in the wild', using WebSphere Portal Server. Try and trace each request, and if *anything* tries to read a request parameter before the correct encoding is set, then the value will be read as ISO8859-1 (if I remember correctly, this is specified in the servlet spec.). – Paul Grime Jun 18 '12 at 21:59
2

Try setting the URIEncoding parameter of your tomcat connector (in the server.xml) to UTF-8:

E.g.:

<Connector port="8080" maxHttpHeaderSize="8192"
           maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
           enableLookups="false" redirectPort="8443" acceptCount="100"
           connectionTimeout="20000" disableUploadTimeout="true"
           URIEncoding="UTF-8"/>
csupnig
  • 3,327
  • 1
  • 24
  • 22