Monday, December 27, 2010

How to change Tomcat default character encoding

We faced an issue last week on linux machine,

In a tomcat deployed server, The non-english characters getting from web services are not displaying correctly.

We write a main program in java and retrieved the data through web-service. its parsed well and correctly displayed the non-english character.

So the issue is with tomcat, after investigation we found this.


In the catalina.bat (catalina.sh) add the java args...

set JAVA_OPTS=-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8

then all encodings seem to come across as UTF-8 by default.. 
This resolved the problem.

source: http://marc.info/?l=tomcat-user&m=108576957301771&w=3

----------------------
+20110113
Added some more info:

Configuring Tomcat's URI encoding

By default, Tomcat uses ISO-8859-1 character encoding when decoding URLs received from a browser. This can cause problems when Confluence's encoding is UTF-8, and you are using international characters in attachment or page names.
  1. Edit conf/server.xml and find the line where the Coyote HTTP Connector is defined. It will look something like this, possibly with more parameters:
    <Connector port="8080"/>
  2. Add a URIEncoding="UTF-8" property to the connector:
    <Connector port="8080" URIEncoding="UTF-8"/>
  3. Restart Tomcat
-------------------
Also check this

9 comments:

  1. Hi David, thanks for this post ! I love it ! It finally solved my issues !

    I had 2 different Linux machines, with THE SAME catalina.sh configuration and with THE SAME application running (the same war file) and even this on 1 server the encoding was OK, and on the other it was NOT OK !! I guess Tomcat takes the setting somehow from the system. But the redefining it by the Java settings you proposed WORKS !

    Thanks a lot again.

    -josef-

    ReplyDelete
  2. Its Pleasure to hear .. Thank God.

    ReplyDelete
  3. Love Love Love!!!

    ReplyDelete
  4. it helped me ... thank you :)

    ReplyDelete
  5. Thanks a lot
    I run as a service on windows and put it into --JvmOptions in service.bat file

    --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;-Djavax.servlet.request.encoding=UTF-8;-Dfile.encoding=UTF-8" ^

    ReplyDelete
  6. .....>Edit conf/server.xml and find the line where the Coyote HTTP Connector is defined. It will look something like this, possibly with more parameters:

    Add a URIEncoding="UTF-8" property to the connector:

    Restart Tomcat.....<

    this saved my day.
    thank you very much.

    ReplyDelete
  7. The second one worked for me ie editing conf/server.xml

    ReplyDelete
  8. But note, that URIEncoding="UTF-8" only applies to GET request. For POST request you may need CharacterEncoding filter - http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q3

    ReplyDelete