I recently ran into a problem encoding web sites created by a servlet that occurred if the servlets were deployed under Tomcat, but not under Jetty. I worked a bit on this research and simplified the problem to the following servlet:
public class TestServlet extends HttpServlet implements Servlet {
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
Writer output = response.getWriter();
output.write("öäüÖÄÜß");
output.flush();
output.close();
}
}
If I deploy it under Jetty and point the browser at it, it returns the expected result. Data is returned as ISO-8859-1, and if I look at the headers, Jetty returns:
Content-Type: text/plain; charset=iso-8859-1
The browser determines the encoding from this header. If I distribute the same servlet in Tomcat, strange characters will appear in the browser. But Tomcat also returns data as ISO-8859-1, the difference is that not a single header says this. Therefore, the browser must guess the encoding, and this is not so.
, Tomcat ? , ? , response.setCharacterEncoding("UTF-8"); , , , . , , , . , ?