How to Declare the Content Language
The ServletResponse interface has a method setLocale that allows you to set the content's locale. It has two effects:
- It sets the Content-Language header field to declare the page's language.
- If you are using a Writer and don't set the character encoding explicitly with setCharacterEncoding, the container will choose an appropriate encoding for your locale (more). In most cases you should call setCharacterEncoding for more predictable behaviour.
Note that the Content-Language is hardly used in browsers, but may be important for search engines.
The following example prints the time in German language and declares it accordingly.
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setLocale(Locale.GERMAN);
PrintWriter writer = response.getWriter();
writer.print("<html><head><title>Zeit</title></head><body>");
writer.printf(Locale.GERMAN, "Die aktuelle Zeit ist: %tc", new Date());
writer.print("</body></html>");
}

