37

I want to get my parameters from the request (characters with accents) but it doesn't work. I tried to user request.setCharacterEncoding("UTF-8") but it didn't work either.

I know that URLDecoder.decode(request.getQueryString(), "UTF-8") returns the right characters, but request.getParameterValues() doesn't work!

Does anyone have an idea?

Attila
  • 3,206
  • 2
  • 31
  • 44
Carvallegro
  • 1,241
  • 4
  • 16
  • 24

3 Answers3

42

Paul's suggestion seems like the best course of action, but if you're going to work around it, you don't need URLEncoder or URLDecoder at all:

String item = request.getParameter("param"); 

byte[] bytes = item.getBytes(StandardCharsets.ISO_8859_1);
item = new String(bytes, StandardCharsets.UTF_8);

// Java 6:
// byte[] bytes = item.getBytes("ISO-8859-1");
// item = new String(bytes, "UTF-8");

Update: Since this is getting a lot of votes, I want to stress BalusC's point that this definitely is not a solution; it is a workaround at best. People should not be doing this.

I don't know exactly what caused the original issue, but I suspect the URL was already UTF-8 encoded, and then was UTF-8 encoded again.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • 13
    To all future readers, please note that this is a workaround, not a solution. In fact, an extremely terrible one which makes your webapp brittle and unportable. – BalusC Dec 27 '14 at 10:07
30

If you are using Apache Tomcat, request.setCharacterEncoding("UTF-8") only works with POST request.

For GET request, you need add URIEncoding="UTF-8" on your <Connector> in server.xml.

See more in FAQ/CharacterEncoding - Apache Tomcat wiki space.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • How about WebSphere Application Server? May I know the server.xml is located at which folder in WAS ? I search for whole, found that there is a lot of server.xml. – Panadol Chong Apr 06 '16 at 06:46
  • With WebSphere, you need to use the system property `client.encoding.override`. See https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzatz/51/admin/help/trun_svr_utf.html – Paul Vargas Dec 26 '17 at 19:32
  • @PaulVargas Hi, the link you provided is now dead, returns 404. Could you kindly update the resource? Thanks! – Sajib Acharya Jul 06 '19 at 23:11
  • 2
    @SajibAcharya Hi! -- Thanks for the heads-up. Apparently, some sort of [migration](https://cwiki.apache.org/confluence/display/TOMCAT/Migrated+Content). But we can see the *original* at [Archive.org](https://web.archive.org/web/20190618043706/https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q2). – Paul Vargas Jul 07 '19 at 00:02
9

Well, I found a way to resolve my problem, I guess that's not the better solution, but it does works..

  String item = request.getParameterValues("param"); // Hélène
  item = URLEncoder.encode( model.getNameItem(), "ISO-8859-1" ); // H%C3%A9l%C3%A8ne
  item = URLDecoder.decode( item, "UTF-8" ); // and finally : Hélène

If it can help anyone else, I'm glad to help :)

PS : Paul, I didn't answer you earlier but I already did those things. Thanks anyway for your help.

Carvallegro
  • 1,241
  • 4
  • 16
  • 24