Search code examples
javaunicodeutf-8character-encodingresponse.redirect

response.sendredirect with url with foreign chars - how to encode?


I have a jsf app that has international users so form inputs can have non-western strings like kanjii and chinese - if I hit my url with ..?q=東日本大 the output on the page is correct and I see the q input in my form gets populated fine. But if I enter that same string into my form and submit, my app does a redirect back to itself after constructing the url with the populated parameters in the url (seems redundant but this is due to 3rd party integration) but the redirect is not encoding the string properly. I have

url = new String(url.getBytes("ISO-8859-1"), "UTF-8");
response.sendRedirect(url);

But url redirect ends up being q=???? I've played around with various encoding strings (switched around ISO and UTF-8 and just got a bunch of gibberish in the url) in the String constructor but none seem to work to where I get q=東日本大 Any ideas as to what I need to do to get the q=東日本大 populated in the redirect properly? Thanks.


Solution

  • How are you making your url? URIs can't directly have non-ASCII characters in; they have to be turned into bytes (using a particular encoding) and then %-encoded.

    URLEncoder.encode should be given an encoding argument, to ensure this is the right encoding. Otherwise you get the default encoding, which is probably wrong and always to be avoided.

    String q= "\u6771\u65e5\u672c\u5927"; // 東日本大
    
    String url= "http://example.com/query?q="+URLEncoder.encode(q, "utf-8");
    // http://example.com/query?q=%E6%9D%B1%E6%97%A5%E6%9C%AC%E5%A4%A7
    
    response.sendRedirect(url);
    

    This URI will display as the IRI ‘http://example.com/query?q=東日本大’ in the browser address bar.

    Make sure you're serving your pages as UTF-8 (using Content-Type header/meta) and interpreting query string input as UTF-8 (server-specific; see this faq for Tomcat.)