64

I have this in web.xml

   <filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

and at the top of file.jsp I have this:

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>

in <head> this:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

and characters other than latin-1 set from FORM with method POST are still not correct.

jbb
  • 1,491
  • 1
  • 10
  • 6

3 Answers3

85

I solved this.

That filter in web.xml must be first filter in file.

jbb
  • 1,491
  • 1
  • 10
  • 6
  • I had the same problem when submitting a web page using Spring and Romanina Characters. I totally missed that it could come because of the way Spring Forms use a different encoding then UTF-8. :) You save me. thx! – Eugene Jul 05 '12 at 14:57
  • 1
    @Sangdol: The problem is that even when pageEncoding is specified to be UTF-8, the modern browsers no longer send the Accept-Encoding header when submitting POST requests (see http://bugzilla.mozilla.org/show_bug.cgi?id=572652, http://code.google.com/p/chromium/issues/detail?id=112804) and the Servlet spec specifies Latin-1/ISO-8859-1 as default. See also the Javadoc for CharacterEncodingFilter: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CharacterEncodingFilter.html – anre Sep 09 '14 at 14:27
  • If you have the problem with Spring security, you can look at that post : http://stackoverflow.com/questions/20863489/characterencodingfilter-dont-work-together-with-spring-security-3-2-0 – Johann Goulley May 21 '16 at 14:11
  • he can't accept. Because his last seen here on stackoverflow shows Mar 3 '11 at 12:46 :p @gipinani – Tahir Hussain Mir Jun 11 '16 at 10:43
  • thank you so much this thing took too much to solve ! – Ali insan Soyaslan Feb 13 '17 at 11:02
14

I had similar problem. When I post a form and save it in DB, it is inserted as ?????? but if I manually insert to DB using the MySQL WorkBench it works fine.

I thought the problem is only in http request encoding. So, I almost implemented all recommendations I found about this issue like change server.xml, adding filter to web.xml and changing settings in MySQL config file my.ini but it does not solve my problem.

The problem was due to two things the http request encoding and the JDBC connection. For some reason MySQL is accepting data as ISO-8859-1 not as UTF-8.

So, I reverted all changes and I made below two changes: Change Tomcat server.xml as below:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"  URIEncoding="UTF-8" />

Change Jdbc connection properties as below:

jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/dB_Name?useUnicode=yes&characterEncoding=UTF-8
jdbc.username=root
jdbc.password

The solution key here is adding useUnicode=yes&characterEncoding=UTF-8

Add a filter like @jbb did in **web.xml:**

<filter>
     <filter-name>encoding-filter</filter-name>
     <filter-class>
  org.springframework.web.filter.CharacterEncodingFilter
     </filter-class>
     <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
     </init-param>
     <init-param>
     <param-name>forceEncoding</param-name>
     <param-value>true</param-value>
     </init-param>
 </filter>

 <filter-mapping>
     <filter-name>encoding-filter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>

If Thymeleaf is used, change viewResolver and TemplateResolver as below:

viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setContentType("text/html; charset=UTF-8");

templateResolver.setCharacterEncoding("UTF-8");
Ayman Al-Absi
  • 2,630
  • 24
  • 22
  • 1
    You've made my day! `URIEncoding="UTF-8"` resolved for me! – shimatai Dec 20 '16 at 00:20
  • i missed encoding-filter in web.xml , now it works like charm. – Swadeshi Sep 13 '17 at 13:40
  • 1
    This: `viewResolver.setCharacterEncoding("UTF-8"); viewResolver.setContentType("text/html; charset=UTF-8"); templateResolver.setCharacterEncoding("UTF-8");` worked for me! Thank's a lot! – Merphys Dec 27 '17 at 14:08
8

Note that this works only for POST requests. If you want to code also GET requests (i.e. links with <a href=...>), you will have to modify your server's server.xml file, by adding URIEncoding="UTF-8" useBodyEncodingForURI="true" attributes in the <Connector> tag.

See : http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

Michel Z
  • 204
  • 4
  • 7
  • Spent a whole morning Googling this issue. Everyone was suggesting the URIEncoding property, first time I see useBodyEncodingForURI and it solved it for me, thank you! – bogdan Aug 09 '17 at 11:31