title: transfer UTF8 input from a JSP form to a Spring controller breaks umlauts
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3665682/utf-8-encoding-and-http-parameters">UTF-8 encoding and http parameters</a>  </p>
  </blockquote>
  
  
  
  <p>I have a UTF8 encoded JSP with a pure UTF8 header (and the text file is also encoded as UTF-8) and a form inside that page:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;   &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;/head&gt;
  &lt;body&gt;
  This is a funny German character: ß
  &lt;form action="utf.do" method="post"&gt;
  &lt;input type="text" name="p" value="${p}" /&gt;
  &lt;input type="submit" value="OK"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then I have a nice Spring-backed @Controller on the backend:</p>
  
  <pre><code>@Controller
  public class UTFCtl {
  @RequestMapping("/utf.do")
  public ModelAndView handleUTF(@RequestParam(value="p", required=false) String anUTFString) {
      ModelAndView ret = new ModelAndView("utf");
      ret.addObject("p", anUTFString);
      return ret;
       }
   }
  </code></pre>
  
  <p>As you see the form transports its data via POST. Typing some German umlauts into the form field yields a bunch of crumbled characters at the backend. So submitting <code>hähöhü</code> on the form field yields <code>hÃ¤hÃ¶hÃ¼</code> as value after submitting. I used the debugger and the var value is already scrambled meaning that Spring/Tomcat/Servlet hasn't detected the encoding correctly or the browser didn't encode my input correctly. The colleagues' usual response to that is: encode in ISO for Germany or encode using Javascript before transmitting. This shouldn't be neccessary, should it?? I mean, this is 2011 and that's what UTF8 is good for!</p>
  
  <p>[EDIT] I think this is proving that the input is incoming as ISO even though I tell him to use UTF8:</p>
  
  <pre><code>byte[] in = anUTFString.getBytes("iso-8859-1");
  String out = new String(in,"UTF-8");
  </code></pre>
  
  <p>out is then displayed correctly in the JSP!</p>
  
  <p>I'm using Spring 2.5 on Tomcat 5.5 with Firefox 4 beta 11 on a Windows XP SP3 box. I already told the Tomcat in its  to use <code>URIEncoding="utf-8"</code> but that doesn't change the game. I analysed the Firefox transmissions using Firebug and it seems to transmit UTF8. I also checked the current Spring WebMVC setup and IMO there are no further encoding changers anywhere, not in the config, nor in the web.xml (no listeners, nothing)- I read and understood most of the UTF-8 related docs and I worked like that in a PHP environment without any problems (simply switching PHP to utf-8, done)...</p>