HttpServletRequest - setCharacterEncoding seems to do nothing

I am trying to read UTF-8 info from the request. I used "request.setCharacterEncoding("UTF-8");", but it seems to do nothing - the info read is non UTF-8.

What am i doing wrong?


If you are using tomcat, you should also set the URIEncoding to UTF-8 in your connectors:

<Server port="8105" shutdown="SHUTDOWN">
...
    <Service name="Catalina">
        <Connector port="8180" URIEncoding="UTF-8" />
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost" appBase="webapps" />
        </Engine>
    </Service>
</Server>

The HttpServletRequest#setCharacterEncoding() has only effect when the request is a POST request and the request body is not processed yet.

So if it doesn't work in your case, then it can have two causes:

  1. You're actually firing a GET request. I.e. the request parameters are sent from client to server in the request URL instead of the request body. The request URL is processed by the webserver, not by the Servlet API. So, to fix this, you need to configure the webserver in question to decode the request URL (URI) using the specified character encoding. In case of for example Apache Tomcat, you need to set the URIEncoding attribute of the <Connector> element in server.xml to UTF-8.

  2. You're correctly using POST, but you've already (indirectly) processed the request body so that it's too late to change the character encoding. The request body will be fully processed only whenever the first call on a getParameterXXX() method is made. There are several of them. It won't be re-processed on subsequent calls. When nailing down who's calling this method, don't forget to take all declared Filter instances in web.xml into account. Some of them might grab and scan the parameters.

If that still doesn't help anything, then the only possible cause left is that the display console or logger or whatever you're using to print/determine/debug the obtained request parameter does not support UTF-8. You'd like to reconfigure the console/logger/etc to use UTF-8 instead to display the characters. If it's for example the Eclipse console, then you can set it by Window > Preferences > General > Workspace > Text File Encoding.

See also:

  • Unicode - How to get characters right? More background info, practical examples and solutions.

this method is really stupid. it shouldn't be there, and you shouldn't use it.

for a body in a POST request, the encoding should have been explicitly defined by the client in the Content-Type header. if not, it's a bad request. [1]

for a GET request URI, the client cannot specify encoding, and the server must have an implicit encoding, and the programmer needs to set the encoding, yet that method does not exist in Servlet API!

however, you servlet container could have a proprietary way of doing that.

the best way is probably set the default encoding of your JVM to UTF-8.

1: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1

The "charset" parameter is used with some media types to define the character set (section 3.4) of the data. When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP. Data in character sets other than "ISO-8859-1" or its subsets MUST be labeled with an appropriate charset value.