Why does the response character encoding in my JSP-based application change after I set it?
In some cases web applications set the response character encoding (which corresponds to the charset value of the content type), but the web page sent to the browser is actually encoded in a different encoding. This problem can occur when using a Servlet 2.3 based container together with the JavaServer Pages Standard Tag Library. The sequence of events is:
- Application sets the content type including a charset value. This sets the response character encoding.
- Application uses a JSTL tag that accesses a resource bundle.
- JSTL sets the response locale to the locale that led to the resource bundle found.
- The container sets the response character encoding to an encoding that's suitable for the response locale, but may be different from the previously set character encoding.
This problem was solved in the Servlet 2.4 specification by distinguishing between explicit and implicit character encoding specifications. Setting the character encoding through the content type or via the new method
ServletResponse.setCharacterEncodingare explicit specifications, while determining it from a locale setting is an implicit specification. Implicit specifications cannot override explicit specifications, so event 4) above does not occur.
If your application needs to be compatible with containers based on older specifications, you must freeze the character encoding by calling
ServletResponse.flushBufferbetween the explicit character encoding specification and the first use of custom actions that might implicitly determine the character encoding.