Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.0-beta-6
-
Fix Version/s: 1.0-beta-9
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
groovy.servlet.TemplateServlet can specify charset of the response by overriding
TemplateServlet#setContentType method, for example as follows:
public class MyTemplateServlet extends TemplateServlet {
protected void setContentType(HttpServletRequest request, HttpServletResponse response)
}
But, it does not work correctly.
Because right now groovy.servlet.TemplateServlet calls TemplateServlet#setContentType AFTER ServletResponse#getWriter,
but ServletResponse#setContentType needs to be called BEFORE ServletResponse#getWriter.
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletResponse.html
The charset for the MIME body response can be specified with
setContentType(java.lang.String). For example, "text/html; charset=Shift_JIS".
The charset can alternately be set using setLocale(java.util.Locale).
If no charset is specified, ISO-8859-1 will be used.
The setContentType or setLocale method must be called before
getWriter for the charset to affect the construction of the writer.
I'll attach a patch for TemplateServlet and GroovyServlet (with some cleanup of unused code).
This problem was originally reported by taedium in http://d.hatena.ne.jp/taedium/20040601#p1 (Japanese).
He's right. There are patches here for TemplateServlet and GroovyServlet to correct the order of the calls to getWriter and setContentType.
In a script, I haven't had a problem overriding the contentType as long it's done before the script prints anything to out. But for extensibility of GroovyServlet and TemplateServlet, the patches here ought to be applied.