Philip Newton (pne) wrote,
Philip Newton
pne

If I had known this earlier... (JSP and "wrong" character encoding on output)

http://java.sun.com/j2se/corejava/intl/reference/faqs/index.html#encoding-override:

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:

  1. Application sets the content type including a charset value. This sets the response character encoding.
  2. Application uses a JSTL tag that accesses a resource bundle.
  3. JSTL sets the response locale to the locale that led to the resource bundle found.
  4. 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.setCharacterEncoding are 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.flushBuffer between the explicit character encoding specification and the first use of custom actions that might implicitly determine the character encoding.

Tags: java, note to self, programming
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments