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)
URL encoding converts characters into a format that can be transmitted over the Internet. - w3Schools. So, "/" is actually a seperator, but "%2f" becomes an ordinary character that simply represents "/" character in element of your url.
For HTTP URLs, a space in a path fragment part has to be encoded to "%20" (not, absolutely not "+"), while the "+" character in the path fragment part can be left unencoded.
The %2C means , comma in URL. when you add the String "abc,defg" in the url as parameter then that comma in the string which is abc , defg is changed to abc%2Cdefg . There is no need to worry about it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With