Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character encoding issue with Tomcat

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

like image 681
OakvilleWork Avatar asked Jun 18 '12 19:06

OakvilleWork


People also ask

What character encoding should I use?

As a content author or developer, you should nowadays always choose the UTF-8 character encoding for your content or data. This Unicode encoding is a good choice because you can use a single character encoding to handle any character you are likely to need. This greatly simplifies things.

How do I change the default charset in Java?

Setting default character encoding or Charset Methods: There are various ways of specifying the default charset value in Java. java -Dfile. encoding="UTF-8" HelloWorld, we can specify UTF-8 charset. Method 2: Specifying the environment variable “JAVA_TOOLS_OPTIONS.”

What does character encoding do?

Character encoding tells computers how to interpret digital data into letters, numbers and symbols. This is done by assigning a specific numeric value to a letter, number or symbol.


2 Answers

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"

like image 188
Rahul Agrawal Avatar answered Oct 05 '22 21:10

Rahul Agrawal


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/[email protected]/msg21117.html.

like image 32
Paul Grime Avatar answered Oct 05 '22 22:10

Paul Grime