4

I set CharacterEncodingFilter as the first filter in my web.xml:

<filter>  
 <filter-name>encodingFilter</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>encodingFilter</filter-name>  
 <url-pattern>/*</url-pattern>  
</filter-mapping> 

and in my JSPs this:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %>

and this:

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

But, for example, passed from a JSP form back to a controller and then back out to a JSP again becomes: T%E

I'm done hours of Google searches but can't find the answer to this problem. Any help would be appreciated.

Jared Owen
  • 41
  • 1
  • 2
  • 1
    Does your controller do anything with the form data such as store it in a database, or does it simply put it back into the JSP? In other words, could this be a problem with the encoding on your database connection? – gutch Dec 17 '10 at 05:36
  • 1
    Are you using ajax to send the form and receive the data? – Javi Dec 17 '10 at 08:18
  • 1
    Are you sure your browser sends the correct headers (you can use Firebug)? Is the method of your form GET or POST? – sinuhepop Dec 17 '10 at 10:57
  • 1
    all three very good questions :) – Bozho Dec 17 '10 at 21:18

2 Answers2

4

If you are on Tomcat you might not have set the URIEncoding in your server.xml. If you don't set it to UTF-8 it won't work. Definitely keep the CharacterEncodingFilter. Nevertheless, here's a concise checklist to follow. It will definitely guide you to make this work.

Community
  • 1
  • 1
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
2

If CharacterEncodingFilter or the filter you implement is at the top of web.xml, be sure to also have the <filter-mapping> elements for the CharacterEncodingFilter in the xml appear before other <filter-mapping> elements for other filters.

Took me a long time to figure that out. Could be useful for someone.