Java EE request params, re-encoding to UTF-8

Just a quick write as i encounter a lot of web applications where the front-end is using UTF-8 encoding to post values to the backend, while the backend, is interpreting them with different (java default iso-8859-1).

Now since Servlet 2.3, there is the Request.setCharacterEncoding(), but unfortunately this doesn’t work if any single parameter has been read before setting the character encoding. In that case, you might do the following:

String someText = request.getParameter("someText");
someText = new String(jsCaseProps.getBytes(Charset.defaultCharset()),request.getCharacterEncoding())

Where Charset.defaultCharset() is probably theĀ ISO-8859-1, as it is default for most application servers.

 

Leave a Reply

Your email address will not be published. Required fields are marked *