You could define a new Filter in your web.xml and modify your answers there.
For example, you could put this in your web.xml :
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.example.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*.jsp</url-pattern>
<!-- or any pattern you like to excluse non-jsp resources -->
</filter-mapping>
And create a com.example.EncodingFilter
class like this :
public class EncodingFilter implements javax.servlet.Filter {
// init, destroy, etc. implementation
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response); // pass request to next filter
}
}
You could furthermore add tests around your setCharacterEncoding()
call to filter only specific URI or exclude for example a non-HTML jsp or specific parameter, like a JSP outputing PDF if URL paramter format=PDF
is provided :
if (!request.getQueryString().contains("format=PDF") {
response.setCharacterEncoding("utf-8");
}
chain.doFilter(request, response); // pass request to next filter