Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
421 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...