The following JSP page will for some strange reason produce a Content-Type header stating ISO-8859-1 (tested with Tomcat 4.1.24). However, if you remove the fmt:formatNumber from the end, it will produce a header with UTF-8 (as expected)! <%@ page language="java" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <% response.setContentType("text/html;charset=UTF-8"); %> <html> <head> <title>Test</title> </head> <body> Test <fmt:formatNumber value="1" pattern="0"/> </body> </html>
Good catch! However, this works fine in Tomcat 5 and therefore appears to be a bug in Tomcat 4.1.24. Please note that the servlet 2.4, JSP 2.0, and JSTL 1.1 specs have all clarified how response encoding should be handled, and it is not surprising that Tomcat 4 might not be behaving as one might expect. At any rate, this is definitely not a bug in JSTL. The fact that the formatting tag triggers a call to response.setLocale() should not modify the response encoding if it has already been set explicitely (as you do in your sample code). For the record, here is how the spec has been clarified in JSTL 1.1: Section 8.4 Any i18n action that establishes a localization context is responsible for setting the response's locale of its page, unless the localization context that was established does not have any locale. This is done by calling method ServletResponse.setLocale() with the locale of the localization context. This assumes that the response is buffered with a big enough buffer size, since ServletResponse.setLocale() and ServletResponse.setContentType() must be called before ServletResponse.getWriter() in order for the specified locale or charset to affect the construction of the writer.(1) More specifically, the response's setLocale() method is always called by the <fmt:setLocale> action (see Section 8.5). In addition, it is called by the following actions: - Any <fmt:bundle> (see Section 8.6) and <fmt:setBundle> (see Section 8.7) action. - Any <fmt:message> action that establishes an i18n localization context - Any formatting action that establishes a formatting locale on its own (see Section 9.3). ***** begin clarification ***** The rules related to the setting of an HTTP response character encoding, Content-Language header, and Content-Type header are clearly defined in the Servlet specification. To avoid any ambiguity, the JSTL and JSP specifications define behavior related to a response's locale and character encoding exclusively in terms of Servlet API calls. It is therefore important to note that, as defined in the Servlet spec, a call to ServletResponse.setLocale() modifies the character encoding of the response only if it has not already been set explicitely by calls to ServletResponse.setContentType() (with CHARSET specified) or ServletResponse.setCharacterEncoding(). Page authors should consult the JSP specification to understand how page directives related to locale and character encoding setting translate into Servlet API calls, and how they impact the final response settings. ***** end clarification *****
*** Bug 23125 has been marked as a duplicate of this bug. ***
*** Bug 25456 has been marked as a duplicate of this bug. ***