POST options using incorrect encoding in JSF 1.2

I had a problem with the encoding encoding in my web application (JSF 1.2, Spring and Tomcat 7) and I ended up with ideas on what to test to see where this is happening.

Whenever I send something like "çã" I get "ÃÃÃÃ": this means that my POSTed data as UTF-8 is converted to ISO-8859-1 somewhere in the life of the JSF cycle.

I know that the wrong conversion is UTF-8 according to ISO-8859-1, which leads to the same conclusion:

System.out.println(new String("çã".getBytes("UTF-8"), "ISO-8859-1")); 

I believe that the wrong conversion is somewhere in the JSF life cycle (maybe earlier?), Because I configured the validator in my MB:

 public void debugValidator(FacesContext context, UIComponent component, Object object) throws ValidationException { System.out.println("debug validator:"); System.out.println(object); System.out.println("\n"); throw new ValidationException("DEBUG: " + object.toString()); } 

and his message is returned as: "DEBUG: çà £"

  • I have on all my .xhtml pages the first line as <?xml version="1.0" encoding="UTF-8"?> .
  • I use Facelets, which according to the BalusC article uses UTF-8 by default
  • Therefore, I did not need this, but I still set up Spring CharacterEncodingFilter in my web.xml to set the request character encoding to UTF-8.
  • I put URIEncoding="UTF-8" in the Tomcat server.xml file to guarantee
  • This is not my browser error, it prints the same in the console, and my environment is UTF-8 .

Do you have any idea what else to check? What could be my wrong assumption?

Thanks in advance!

+4
source share
3 answers

BalusC's answer helped me better understand the problem, but for me it decided to put the character encoding filter as a FIRST filter in the chain (putting it above everyone else on the network. Xml).

This is the filter I used:

 <!-- filter enforcing charset UTF-8 - must be first filter in the chain! --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

Apparently, the data was read before the parameter was set by the filter. I got a hint from this page: http://tech.top21.de/techblog/20100421-solving-problems-with-request-parameter-encoding.html

Thanks everyone!

+7
source

Symptoms indicate that the browser sent data using ISO-8859-1 encoding instead of UTF-8. This, in turn, means that the Content-Type HTTP response header is not set with the corresponding charset attribute. For example, Firebug you can find it like this:

enter image description here

You are correct that Facelets uses UTF-8 by default. But very early versions of Facelets were not programmed to use UTF-8 by default. See also, among others, issue 46 and issue 53 . Facelets is currently located at 1.1.15.B1 .

As for your attempts to fix this, the presence of the XML prolog is not strictly necessary and its encoding is not used in any way to set the response encoding, it is used only by the XML parser to decode the input stream for characters. Spring filter is also not needed, but it didn’t solve the problem after you added it, there is enough evidence that it is the client who sent the data as ISO-8859-1.

+3
source

Check if your form has enctype="multipart/form-data" .

See this question for more details.

+1
source

Source: https://habr.com/ru/post/1387642/


All Articles