{"id":336,"date":"2010-08-12T08:32:25","date_gmt":"2010-08-12T06:32:25","guid":{"rendered":"http:\/\/touk.pl\/blog\/?p=336"},"modified":"2023-03-20T13:03:40","modified_gmt":"2023-03-20T12:03:40","slug":"336","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/08\/12\/336\/","title":{"rendered":"SpringWeb  + Hibernate + Database encoding problems &#8211; solved!"},"content":{"rendered":"<p><!-- p, li { white-space: pre-wrap; } --><\/p>\n<p style=\"text-align: justify\"><!--StartFragment-->Some time ago, I was investigating SmartClient (SmartGWT) and writting small app to connect via hibernate to MySQL database. During this tests I stuck with Polish locale problem. Such problems I&#8217;ve been facing not only this time, but many times in my career. Today, though it&#8217;s pretty easy, I will describe once for all how to solve the problem with web apps and Polish (but also any non english) encoding.<\/p>\n<p><!--:--><\/p>\n<p><!--more--><\/p>\n<p><!--:en-->The first thing to be done is to set UTF-8 encodings in all web frontend files (*.html\/*.jsp etc) by adding following code into tags:<\/p>\n<p style=\"text-align: justify\">Ok, this was easy and has nothing to do with database, but at least it makes sure, that localized characters will be displayed in proper way in the browser.<\/p>\n<p>Second thing to do is to write filter which will set UTF-8 coding to requests and responses. It could look this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ToukEncodingFilter extends OncePerRequestFilter {\r\n\r\n    @Override\r\n    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {\r\n\r\n        response.setCharacterEncoding(\"UTF-8\");\r\n\r\n        request.setCharacterEncoding(\"UTF-8\");\r\n        chain.doFilter(request, response);\r\n\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now, to make this filter running, the following lines have to be add to web.xml file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">CharacterEncodingFilter \r\npl.touk.filters.ToukEncodingFilter \r\nCharacterEncodingFilter \r\n\/*<\/pre>\n<p style=\"text-align: justify\">Ok. So we have now all in web layer prepared. Now, to make it work with DB, the hibernate connection have to be configured like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">url=jdbc:mysql:\/\/\/?useUnicode=true&amp;characterEncoding=UTF-8<\/pre>\n<p style=\"text-align: justify\">It can be also done via hibernate properties.<\/p>\n<p style=\"text-align: justify\">Of course I assume, that database has UTF-8 encoding. I won&#8217;t write about this, becouse it is pretty well written all over internet. Just a little tip: it is easier to set this before creating any table ;-)<\/p>\n<p style=\"text-align: justify\">Last, but the most important thing, is to set jvm encoding parameters to UTF-8 and desired locale. If You are using Tomcat It can be done by modifying CATALINA_OPTS variable (in init script) by adding following options:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">-Dfile.encoding=UTF-8 -Duser.country=PL -Duser.language=pl<\/pre>\n<p style=\"text-align: justify\">That&#8217;s all!<!--EndFragment--><\/p>\n","protected":false},"excerpt":{"rendered":"Some time ago, I was investigating SmartClient (SmartGWT) and writting small app to connect via hibernate to MySQL&hellip;\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[687,71,43,68,13,42],"class_list":{"0":"post-336","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-db","8":"tag-frontend","9":"tag-hibernate","10":"tag-java","11":"tag-oracle","12":"tag-spring-framework"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":19,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":15358,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions\/15358"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}