Issue
I have a Servlet that process a QueryString
from a JSP form.
When the JSP send the information to the Servlet, it puts on the URL the query, like this:
https://www.mywebsite.com/search?query=Barcelona
And i receive the data inside the Servlet in this way:
String query = request.getQueryString();
// query will receive "Barcelona"
Everything works alright. The problem comes when the string contains characters from other encodings like russian, chinese, arabic, etc. So, if I put the URL like:
https://www.mywebsite.com/search?query=Медеуский
I will receive in the query variable the following text:
String query = request.getQueryString();
// query will receive " %D0%9C%D0%B5%D0%B4%D0%B5%D1%83%D1%81%D0%BA%D0%B8%D0%B9"
So, how could I avoid the escaped conversion?
Thanks!
Solution
You can use URLDecoder.decode(request.getQueryString(), "UTF-8")
or
- String unicodeQuery = new String(query.getBytes(), StandardCharsets.UTF_8);
- String unicodeQuery = new String(query.getBytes("ISO_8859_1"), StandardCharsets.UTF_8);
- String unicodeQuery = new String(query.getBytes(Charset.defaultCharset()), StandardCharsets.UTF_8);
Answered By - Toàn Nguyễn Hải
Answer Checked By - Pedro (JavaFixing Volunteer)