14

I instruct my URL to send an Ajax request like that:

url += '/' + something + '/' + id;
var response;
$.ajax({
    async : false,
    type: 'DELETE',
    url: url,
 ...

My removeId is a variable that includes UTF-8 character. I will handle that variable at Java side like that:

@RequestMapping(value = "/something/{id}", method = RequestMethod.DELETE)
    public void myMethod(HttpServletResponse response, @PathVariable String id) {
    ...

However id variable at Java side is different from its original because UTF-8 characters changes to strange things.

How can I send UTF-8 characters from JavaScript side and transform it again at my Java side (Spring 3 with REST, my web server is Tomcat 7)?

PS 1: Even I don't use encodeUriComponent it seems that my URL is encoding by itself?

PS 2: To make question more clear:

i.e. my id variable is araç and sent URL is: localhost:8080/sdfasf/ara%C3%A7 

When I see that id variable has that value:

araç

instead of:

ara%C3%A7 

Does Spring (or Tomcat) automatically do it? Is there any way to decode it automatically when it comes to controller as a path variable (I mean without writing anything as like:

URLDecoder.decode(id,"UTF-8");

it will be converted automatically)

informatik01
  • 16,038
  • 10
  • 74
  • 104
kamaci
  • 72,915
  • 69
  • 228
  • 366

6 Answers6

16

The id value you see seems to be decoded using the iso-8859-1 charset instead of utf-8. The encoding of the path part of a url is not specified in java EE and there is no standard api to set it. For query parameters you can use request.setCharacterEncoding before accessing any parameters to have them decoded correctly. The CharacterEncodingFilter does exactly that but has no influence on path parameters.

To make this work in Tomcat you have to set the URIEncoding attribute of the Connector element in its server.xml to "utf-8".

All you ever wanted to know about character encoding in a java webapp can be found in this excellent answer to a similar question.

Community
  • 1
  • 1
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • 1
    thanks for your answer and voting up. I just want to sure. I use Spring and did that configuration (didn't do anything with Tomcat) and I think that I resolved my problem. Do I miss something (I mean maybe I didn't resolve the problem exactly and some characters will give error too?) and is there any example for how to use request.setCharacterEncoding? – kamaci Nov 16 '11 at 21:33
  • @kamaci: I did not realize that was your answer and that it already solved your problem when I posted my answer. Calling `setCharacterEncoding` did not work for path parameters with oracle's OC4j container, maybe it does with tomcat. Although that contradicts some answers to similar questions like http://stackoverflow.com/questions/8079152/charset-filter-causing-issue-in-parsing-utf-8-characters/8079547#8079547 or http://stackoverflow.com/questions/2630748/how-to-enable-reading-non-ascii-characters-in-servlets/2630977#2630977 – Jörn Horstmann Nov 16 '11 at 22:31
3

I found the answer here: http://forum.springsource.org/showthread.php?14063-How-to-set-setCharacterEncoding-on-Request

I did that configuration at my Spring side:

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

and it works fine for UTF-8.

kamaci
  • 72,915
  • 69
  • 228
  • 366
1

Have you tried setting page encoding on the page where you are embedding the javascript?

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
Udo Held
  • 12,314
  • 11
  • 67
  • 93
1

Make sure the page encoding is set to UTF-8 by adding this right below the starting <head> tag:

<meta charset="utf-8">

Also make sure the JavaScript file is saved with UTF-8 encoding. Check your text editor settings.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
0

In JavaScript use:

data=encodeURIComponent(data);

for encoding data, and in Java you can do:

data=java.net.URLDecoder.decode(data);
Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • In the question the id is part of the request path so it already gets decoded by the servlet container. Also, the `decode` method should be called with the second parameter specifying the charset to use. – Jörn Horstmann Nov 16 '11 at 22:38
-1

No need to encode the string on javascript side. Java does not support unicode so decoding needs to be done there as:

output = new String(input.getBytes("ISO-8859-1"), "UTF-8");

p3drosola
  • 5,784
  • 2
  • 23
  • 30