6

I have everything in UTF-8. That includes Content-Type, database, files, java, everything (unless I've missed something).

I follow a lot of stackoverflow answers, JIRAs, blogs, and etc, but, it still failing.

The problem itself is the following:

When I submit, let's suppose, to http://localhost:8080/app/searh?text=café, debugging, my request.getParameter("text") is always wrong, something like café, and request.getCharachterEncoding() gives me null (?).

Looking at the request headers, I got this:

GET http://localhost:8080/app/search?text=caf%C3%A9 HTTP/1.1
Host: localhost:8080
Proxy-Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.69 Safari/537.17
Referer: http://localhost:8080/app/search?text=n%C3%A3o
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: UTF-8,*;q=0.5
Cookie: JSESSIONID=OMMITED

And the response headers:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Wed, 31 Dec 1969 21:00:00 BRT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Tue, 19 Mar 2013 14:06:24 GMT
Proxy-Connection: keep-alive
Connection: keep-alive

It's everything UTF-8. I just don't understand.

I tried to pass -Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8 in my standalone.conf JAVA_OPTS variable, tried to put

<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>

in my standalone.xml. Nothing of this solves the issue.

What can I try to do to fix this?

Thanks in advance.

BTW: Is a JBoss AS 7.1.1.

caarlos0
  • 20,020
  • 27
  • 85
  • 160

5 Answers5

3

I came into the same issue but in Jboss 5.1, and I solved it adding the URIEncoding attribute to the HTTP Connector (in jbossweb/server.xml) and decoding the URL/GET parameters manually.

But the way to define it in Jboss7 is different from previous versions, but googling a bit I found this link: basically you've to add the following lines in the standalone.xml or domain.xml file after the end of the </extensions> tag (it looks like you've already done this step ;-):

<system-properties>
     <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
     <property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>

Moreover you've to decode manually the URI or parameter with the help of the java.net.URIEncoder object:

String param = URLDecoder.decode(request.getParameter("text"), "UTF-8");

BalusC has an interesing post in his blog about it.

And finally, a second solution, if you want to avoid using the previous options: have you considered using the POST method instead of the GET one?

I hope it helps.

Toni
  • 1,381
  • 10
  • 16
  • Hi, thanks for your answer; The first solution I already tried, didn't work. Using POST will not made sense in my case, because is a search, and I want that users could copy the URL. I also tried your second solution, and the bug persists as well. Pretty creepy... Thanks – caarlos0 Mar 19 '13 at 16:51
  • 1
    Hi Caarlos, I answered by heart, but I was a bit curious about your problem, and now I've prepared an example: I've reproduced your problem and I've finally made it work, and the solution works in both Jboss 5.x and 7. The fact is that the first two solutions I've given are really one single solution, so to make it work you've to to define the systems properties, and moreover you've to decode the parameter. I've modified the answer. – Toni Mar 19 '13 at 20:58
  • Thanks, I'll consider that =D – caarlos0 Mar 21 '13 at 18:07
  • 1
    Let me know if you experience any problem :) – Toni Mar 21 '13 at 20:45
2

I solved the issue creating a Filter that set both request and response encoding to UTF-8.

Pretty hacky, but works.

caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • Yep, I'm using JBoss EAP 6.1 and had to use `request.setCharacterEncoding("UTF-8")` besides setting the `catalina.*` properties – tbrugz Jun 10 '15 at 15:43
1

I don't have enough reputation to add a comment to the answer by Toni S. Magraner, so I'll write here.

request.getParameter("text") already does the URL decoding. Calling URLDecoder.decode() again will give double decoding which will probably not do what you want. An example:

logger.error("p1:"+request.getParameter("p1"));

Calling it with

http://localhost/test?p1=ku%2fki%44__%33X%C3%A9X

prints:

p1:ku/kiD__3XéX
David Balažic
  • 1,319
  • 1
  • 23
  • 50
0

The standalone.xml or domain.xml configuration doesn't work for me.

On jboss-as-7.1.1.Final just add this line to standalone.conf, this file lives under the directory bin:

JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.connector.URI_ENCODING=UTF-8"

from: JBOSS 7 encoding not working as expected

Community
  • 1
  • 1
erivas
  • 21
  • 1
  • 4
0

We had a similar problem in POST requests, with parameters in request body:

p1=v2&text=café

We solved client-side adding a header in request:

Content-Type: application/x-www-form-urlencoded; charset=UTF-8
luca.vercelli
  • 898
  • 7
  • 24