So how does Spring MVC set the response encoding of a JSP to UTF-8? It's a trick question. The short answer is that it doesn't!
Spring MVC supports many different view technologies such as JSP for HTML, Jackson for JSON, Rome for ATOM. With most view technologies Spring MVC will first try to determine the MIME type of the response, including the character encoding, and set it in the HTTP header field named Content-Type. Only then will it forward to the view technology that renders the HTTP body.
However for JSPs specifically Spring MVC has no influence on the Content-type field.
JSPs are prepared on the Spring side by an instance of the JstlView class. Since JSP support is already built into the servlet container, all JstlView has to do is forward to the JSP with RequestDispatcher.forward(). The Servlet container then translates the JSP into a Servlet and compiles it. One of the first things the Servlet does, when its service() method is invoked on a HTTP request, is to set the response Content-Type. This overrides any previous efforts by Spring MVC to affect the value of this field.
In other words the encoding is set according to the standard spec for JSP and Servlets which can be summed up as follows:
To set the encoding of all JSPs to UTF-8 add this snippet to web.xml:
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config>
The URL-pattern from the snippet above will not match pages with underscores, such as my_page.jsp, on Apache Tomcat!
Another option is to use the JSP page directive to set the encoding on individual pages:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Don't forget to UTF-8 encode the JSP file. In Eclipse, for instance, you would right-click the file, select properties and choose "Text file encoding" UTF-8.