How to set up UTF-8 encoding for URIs in JBoss and Tomcat

I was surprised to find out Jboss (Tomcat) servlet container does not use UTF-8 URI encoding by default.   What this means is, in a web application running under JBoss or Tomcat, query strings in GET requests that contain multi-byte UTF-8 encoded characters (Chinese or Japanese characters, for example) will not be decoded accurately on server side.

I ran into this issue on the target page of a POST-REDIRECT-GET invocation.  The requirement was to display some user data on the target page and the data was being passed as query string of the GET call.  I could see that the query string was properly URI-encoded, but it was not getting displayed properly on the target page.  Upon investigating, I figured out this is neither an application bug  nor a limitation of the web framework that was being used (Struts 2 in this case).  The culprit was JBoss’s servlet container, which was not using UTF-8 decoding on query strings.

Here is how you fix this problem.  Locate server.xml configuration file used by the JBoss server.  You will find it under <JBoss home>/server/<server-name>/deploy/jboss-web.deployer.  If you are using plain Tomcat, you will find this under <Tomcat home>/conf.   Add the attribute URIEncoding=”UTF-8″ to the appropriate connector element and re-start the server.  That should do the trick!

About these ads

Tags:

One Response to “How to set up UTF-8 encoding for URIs in JBoss and Tomcat”

  1. Friend Says:

    Thank you so much for pointing to this. Actually, I have same problem where the redirect-get was creating junk chinese characters. I had the URIEncoding set which caused the problem and once I removed it, it is working fine :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: