Character encoding of ServletOutputStream print methods
- Dominant language
- Java
- Stars
- 325
- Forks
- 112
- PR merge metrics
- No merged PRs in 30d
Description
The async API is only available on the output stream. Thus we have had a framework that used to do:
response.setContentType("application/json;charset=UTF-8");
...
response.getWriter().print("Ӝ");
convert to asynchronous style and now it does:
response.setContentType("application/json;charset=UTF-8");
...
response.getOutputStream().print("Ӝ");
This works fine on some containers (eg Jetty), but apparently many containers do not respect the encoding set by the setContentType. They either convert the character as ISO8859-1 or if the first character is UTF-8, they throw a CharacterEncoding exception.
We have also seen at least one container handle the following writes out of order:
response.getOutputStream().print("one");
response.getOutputStream().write("two".getBytes(UTF_8);
response.getOutputStream().print("three");
As the async API will now make more character oriented frameworks use the output stream, I believe that we need to clarify the semantics of the ServletOutputStream print method so that it:
+ is definitely mix and match with write - so we must say that print cannot buffer any data.
+ respects the character encoding set on the response
Contributor guide
Assessment
This issue has not been assessed yet.