Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Answering myself as the FAQ of this site encourages it. This works for me: </em></p> <p>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.</p> <p>To get UTF-8 working under Java+Tomcat+Linux/Windows+Mysql requires the following:</p> <h2>Configuring Tomcat's server.xml</h2> <p>It's necessary to configure that the connector uses UTF-8 to encode url (GET request) parameters:</p> <pre><code>&lt;Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" compression="on" compressionMinSize="128" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/plain,text/css,text/ javascript,application/x-javascript,application/javascript" URIEncoding="UTF-8" /&gt; </code></pre> <p>The key part being <b>URIEncoding="UTF-8"</b> 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:</p> <pre><code> https://localhost:8443/ID/Users?action=search&amp;name=*ж* </code></pre> <p>the character ж is handled as UTF-8 and is encoded to (usually by the browser before even getting to the server) as <b>%D0%B6</b>.</p> <p><i>POST request are not affected by this.</i></p> <h2> CharsetFilter </h2> <p>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:</p> <pre><code>package fi.foo.filters; import javax.servlet.*; import java.io.IOException; public class CharsetFilter implements Filter { private String encoding; public void init(FilterConfig config) throws ServletException { encoding = config.getInitParameter("requestEncoding"); if (encoding == null) encoding = "UTF-8"; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain next) throws IOException, ServletException { // Respect the client-specified character encoding // (see HTTP specification section 3.4.1) if (null == request.getCharacterEncoding()) { request.setCharacterEncoding(encoding); } // Set the default response content type and encoding response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); next.doFilter(request, response); } public void destroy() { } } </code></pre> <p>This filter makes sure that if the browser hasn't set the encoding used in the request, that it's set to UTF-8. </p> <p>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. </p> <p>This filter has to be added to the <b>web.xml</b> or the deployment descriptor of the webapp:</p> <pre><code> &lt;!--CharsetFilter start--&gt; &lt;filter&gt; &lt;filter-name&gt;CharsetFilter&lt;/filter-name&gt; &lt;filter-class&gt;fi.foo.filters.CharsetFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;requestEncoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;CharsetFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>The instructions for making this filter are found at the <a href="http://wiki.apache.org/tomcat/Tomcat/UTF-8" rel="nofollow noreferrer">tomcat wiki (<a href="http://wiki.apache.org/tomcat/Tomcat/UTF-8" rel="nofollow noreferrer">http://wiki.apache.org/tomcat/Tomcat/UTF-8</a>)</a> </p> <h2> JSP page encoding </h2> <p>In your <strong>web.xml</strong>, add the following:</p> <pre><code>&lt;jsp-config&gt; &lt;jsp-property-group&gt; &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt; &lt;page-encoding&gt;UTF-8&lt;/page-encoding&gt; &lt;/jsp-property-group&gt; &lt;/jsp-config&gt; </code></pre> <p>Alternatively, all JSP-pages of the webapp would need to have the following at the top of them:</p> <pre><code> &lt;%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%&gt; </code></pre> <p>If some kind of a layout with different JSP-fragments is used, then this is needed in <b>all</b> of them.</p> <h2> HTML-meta tags </h2> <p>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:</p> <p>This is done with the following at the top of each xhtml page produced by the webapp:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"&gt; &lt;head&gt; &lt;meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /&gt; ... </code></pre> <h2> JDBC-connection </h2> <p>When using a db, it has to be defined that the connection uses UTF-8 encoding. This is done in <b>context.xml</b> or wherever the JDBC connection is defiend as follows: </p> <pre><code> &lt;Resource name="jdbc/AppDB" auth="Container" type="javax.sql.DataSource" maxActive="20" maxIdle="10" maxWait="10000" username="foo" password="bar" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/ ID_development?useEncoding=true&amp;amp;characterEncoding=UTF-8" /&gt; </code></pre> <h2> MySQL database and tables</h2> <p>The used database must use UTF-8 encoding. This is achieved by creating the database with the following:</p> <pre><code> CREATE DATABASE `ID_development` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_swedish_ci */; </code></pre> <p>Then, all of the tables need to be in UTF-8 also:</p> <pre><code> CREATE TABLE `Users` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(30) collate utf8_swedish_ci default NULL PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci ROW_FORMAT=DYNAMIC; </code></pre> <p>The key part being <b>CHARSET=utf8</b>.</p> <h2> MySQL server configuration </h2> <p>MySQL serveri has to be configured also. Typically this is done in Windows by modifying <b>my.ini</b> -file and in Linux by configuring <b>my.cnf</b> -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.</p> <pre><code> [client] port=3306 default-character-set=utf8 [mysql] default-character-set=utf8 </code></pre> <h2> Mysql procedures and functions </h2> <p>These also need to have the character set defined. For example:</p> <pre><code> DELIMITER $$ DROP FUNCTION IF EXISTS `pathToNode` $$ CREATE FUNCTION `pathToNode` (ryhma_id INT) RETURNS TEXT CHARACTER SET utf8 READS SQL DATA BEGIN DECLARE path VARCHAR(255) CHARACTER SET utf8; SET path = NULL; ... RETURN path; END $$ DELIMITER ; </code></pre> <h2> GET requests: latin1 and UTF-8 </h2> <p>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:</p> <pre><code> https://localhost:8443/ID/Users?action=search&amp;name=Petteri https://localhost:8443/ID/Users?action=search&amp;name=ж </code></pre> <p>Because ASCII-characters are encoded in the same way both with latin1 and UTF-8, the string "Petteri" is handled correctly.</p> <p>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 <b>%D0%B6</b>. </p> <p>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 <b>%D0%B6</b>. </p> <p>The end result is that all users with name "Petteri" are found and also all users with the name "ж" are found.</p> <h3>But what about äåö?</h3> <p>HTTP-specification defines that by default URLs are encoded as latin1. This results in firefox2, firefox3 etc. encoding the following</p> <pre><code> https://localhost:8443/ID/Users?action=search&amp;name=*Päivi* </code></pre> <p>in to the encoded version</p> <pre><code> https://localhost:8443/ID/Users?action=search&amp;name=*P%E4ivi* </code></pre> <p>In latin1 the character <b>ä</b> is encoded as <b>%E4</b>. <em>Even though the page/request/everything is defined to use UTF-8</em>. The UTF-8 encoded version of ä is <b>%C3%A4</b></p> <p>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. <b>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</b></p> <h2> Stuff to read </h2> <p>A very big thank you for the writers of the following for giving the answers for my problem:</p> <ul> <li> http://tagunov.tripod.com/i18n/i18n.html </li> <li> http://wiki.apache.org/tomcat/Tomcat/UTF-8 </li> <li> http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/ </li> <li> http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html </li> <li> http://cagan327.blogspot.com/2006/05/utf-8-encoding-fix-tomcat-jsp-etc.html </li> <li> http://cagan327.blogspot.com/2006/05/utf-8-encoding-fix-for-mysql-tomcat.html </li> <li> http://jeppesn.dk/utf-8.html </li> <li> http://www.nabble.com/request-parameters-mishandle-utf-8-encoding-td18720039.html </li> <li> http://www.utoronto.ca/webdocs/HTMLdocs/NewHTML/iso_table.html </li> <li> http://www.utf8-chartable.de/ </li> </ul> <h2>Important Note</h2> <p><a href="/questions/tagged/mysql" class="post-tag" title="show questions tagged &#39;mysql&#39;" rel="tag">mysql</a> supports the <a href="http://en.wikipedia.org/wiki/Plane_%28Unicode%29#Basic_Multilingual_Plane" rel="nofollow noreferrer">Basic Multilingual Plane</a> 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 <code>VARBINARY</code> column type or use the <a href="http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html" rel="nofollow noreferrer"><code>utf8mb4</code> character set</a> (which requires MySQL 5.5.3 or later). Just be aware that using the <code>utf8</code> character set in MySQL won't work 100% of the time.</p> <h2>Tomcat with Apache</h2> <p>One more thing If you are using Apache + Tomcat + mod_JK connector then you also need to do following changes:</p> <ol> <li>Add URIEncoding="UTF-8" into tomcat server.xml file for 8009 connector, it is used by mod_JK connector. <code>&lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/&gt;</code></li> <li>Goto your apache folder i.e. <code>/etc/httpd/conf</code> and add <code>AddDefaultCharset utf-8</code> in <code>httpd.conf file</code>. <strong>Note:</strong> 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.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload