I need to get UTF-8 working in my Java webapp (servlets + JSP, no framework used) to support äöå
etc. for regular Finnish text and Cyrillic alphabets like ЦжФ
for special cases.
My setup is the following:
- Development environment: Windows XP
- Production environment: Debian
Database used: MySQL 5.x
Users mainly use Firefox2 but also Opera 9.x, FF3, IE7 and Google Chrome are used to access the site.
How to achieve this?
About
CharsetFilter
mentioned in @kosoant answer ....There is a build in
Filter
in tomcatweb.xml
(located atconf/web.xml
). The filter is namedsetCharacterEncodingFilter
and is commented by default. You can uncomment this ( Please remember to uncomment itsfilter-mapping
too )Also there is no need to set
jsp-config
in yourweb.xml
(I have test it for Tomcat 7+ )Previous responses didn't work with my problem. It was only in production, with tomcat and apache mod_proxy_ajp. Post body lost non ascii chars by ? The problem finally was with JVM defaultCharset (US-ASCII in a default instalation: Charset dfset = Charset.defaultCharset();) so, the solution was run tomcat server with a modifier to run the JVM with UTF-8 as default charset:
(add this line to catalina.sh and service tomcat restart)
Maybe you must also change linux system variable (edit ~/.bashrc and ~/.profile for permanent change, see https://perlgeek.de/en/article/set-up-a-clean-utf8-environment)
I think you summed it up quite well in your own answer.
In the process of UTF-8-ing(?) from end to end you might also want to make sure java itself is using UTF-8. Use -Dfile.encoding=utf-8 as parameter to the JVM (can be configured in catalina.bat).
Answering myself as the FAQ of this site encourages it. This works for me:
Mostly characters äåö are not a problematic as the default character set used by browsers and tomcat/java for webapps is latin1 ie. ISO-8859-1 which "understands" those characters.
To get UTF-8 working under Java+Tomcat+Linux/Windows+Mysql requires the following:
Configuring Tomcat's server.xml
It's necessary to configure that the connector uses UTF-8 to encode url (GET request) parameters:
The key part being URIEncoding="UTF-8" in the above example. This quarantees that Tomcat handles all incoming GET parameters as UTF-8 encoded. As a result, when the user writes the following to the address bar of the browser:
the character ж is handled as UTF-8 and is encoded to (usually by the browser before even getting to the server) as %D0%B6.
POST request are not affected by this.
CharsetFilter
Then it's time to force the java webapp to handle all requests and responses as UTF-8 encoded. This requires that we define a character set filter like the following:
This filter makes sure that if the browser hasn't set the encoding used in the request, that it's set to UTF-8.
The other thing done by this filter is to set the default response encoding ie. the encoding in which the returned html/whatever is. The alternative is to set the response encoding etc. in each controller of the application.
This filter has to be added to the web.xml or the deployment descriptor of the webapp:
The instructions for making this filter are found at the tomcat wiki (http://wiki.apache.org/tomcat/Tomcat/UTF-8)
JSP page encoding
In your web.xml, add the following:
Alternatively, all JSP-pages of the webapp would need to have the following at the top of them:
If some kind of a layout with different JSP-fragments is used, then this is needed in all of them.
HTML-meta tags
JSP page encoding tells the JVM to handle the characters in the JSP page in the correct encoding. Then it's time to tell the browser in which encoding the html page is:
This is done with the following at the top of each xhtml page produced by the webapp:
JDBC-connection
When using a db, it has to be defined that the connection uses UTF-8 encoding. This is done in context.xml or wherever the JDBC connection is defiend as follows:
MySQL database and tables
The used database must use UTF-8 encoding. This is achieved by creating the database with the following:
Then, all of the tables need to be in UTF-8 also:
The key part being CHARSET=utf8.
MySQL server configuration
MySQL serveri has to be configured also. Typically this is done in Windows by modifying my.ini -file and in Linux by configuring my.cnf -file. In those files it should be defined that all clients connected to the server use utf8 as the default character set and that the default charset used by the server is also utf8.
Mysql procedures and functions
These also need to have the character set defined. For example:
GET requests: latin1 and UTF-8
If and when it's defined in tomcat's server.xml that GET request parameters are encoded in UTF-8, the following GET requests are handled properly:
Because ASCII-characters are encoded in the same way both with latin1 and UTF-8, the string "Petteri" is handled correctly.
The Cyrillic character ж is not understood at all in latin1. Because Tomcat is instructed to handle request parameters as UTF-8 it encodes that character correctly as %D0%B6.
If and when browsers are instructed to read the pages in UTF-8 encoding (with request headers and html meta-tag), at least Firefox 2/3 and other browsers from this period all encode the character themselves as %D0%B6.
The end result is that all users with name "Petteri" are found and also all users with the name "ж" are found.
But what about äåö?
HTTP-specification defines that by default URLs are encoded as latin1. This results in firefox2, firefox3 etc. encoding the following
in to the encoded version
In latin1 the character ä is encoded as %E4. Even though the page/request/everything is defined to use UTF-8. The UTF-8 encoded version of ä is %C3%A4
The result of this is that it's quite impossible for the webapp to correly handle the request parameters from GET requests as some characters are encoded in latin1 and others in UTF-8. Notice: POST requests do work as browsers encode all request parameters from forms completely in UTF-8 if the page is defined as being UTF-8
Stuff to read
A very big thank you for the writers of the following for giving the answers for my problem:
Important Note
mysql supports the Basic Multilingual Plane using 3-byte UTF-8 characters. If you need to go outside of that (certain alphabets require more than 3-bytes of UTF-8), then you either need to use a flavor of
VARBINARY
column type or use theutf8mb4
character set (which requires MySQL 5.5.3 or later). Just be aware that using theutf8
character set in MySQL won't work 100% of the time.Tomcat with Apache
One more thing If you are using Apache + Tomcat + mod_JK connector then you also need to do following changes:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
/etc/httpd/conf
and addAddDefaultCharset utf-8
inhttpd.conf file
. Note: First check that it is exist or not. If exist you may update it with this line. You can add this line at bottom also.Nice detailed answer. just wanted to add one more thing which will definitely help others to see the UTF-8 encoding on URLs in action .
Follow the steps below to enable UTF-8 encoding on URLs in firefox.
type "about:config" in the address bar.
Use the filter input type to search for "network.standard-url.encode-query-utf8" property.
UTF-8 encoding on URLs works by default in IE6/7/8 and chrome.
In case you have specified in connection pool (mysql-ds.xml), in your Java code you can open the connection as follows: