Say I have a string with "é" in it, and I want to send it over a URL to the next controller, the characters gets encoded to %C3%A9, and when it's received in the other controller, it gets decoded to "é". My question is, how to encode the "é" over the URL so when it's received in the other controller it gets decoded to "é"? For now, I'm replacing them manually. I need a way to do it automatically and with any special character (éèà...) Thank you.
-
[`URLEncoder`](https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html) and [`URLDecoder`](https://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html) ? – KarelG Sep 27 '17 at 09:01
-
Possible duplicate of [HTTP URL Address Encoding in Java](https://stackoverflow.com/questions/724043/http-url-address-encoding-in-java) – Pablo Lozano Sep 27 '17 at 09:06
1 Answers
Unfortunately there is no way to declare the encoding of URL data. The common encoding used to be ISO-8859-1 or Latin1, but nowadays, UTF-8 is often used in new developments. The reasons was that the servlet specification says that when the charset is not specified, ISO-8859-1 is implied, but HTML 4.0 recommends UTF-8 for URLs.
The problem is that the URL is composed of bytes and that the servlet container convert it to java characters before passing it to the application, so you must declare the used charset at the servlet container level. For compatibily reasons, the well known Tomcat before version 8.0 assumed by default a Latin1 charset for the URL. Since 8.0.0 the default now depends on the "strict servlet compliance" setting. It is ISO-8859-1 when true and UTF-8 when false
References:
- What character set should I assume the encoded characters in a URL to be in?
- Tomcat FAQ/CharacterEncoding
For your precise question, you have two ways to correctly process the é
character:
- leave your servlet container configuration unchanged and encode the URL in ISO-8859-1 (
é
will be%E9
) - stick to UTF-8 in the URL but declare it to the servlet container

- 143,923
- 11
- 122
- 252