Search code examples
javaservletsutf-8tomcat6iso-8859-1

Override tomcat connector URIEncoding


I develop an application that's deployed to a tomcat instance that also hosts other applications. For their purpose, the connector on port 80 has its URIEncoding set to "UTF-8". I would like to find a way to override this setting for my servlet, without specifying another connector on another port. I have tried using a tomcat filter to set the filter as follows:

@Override
public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain filterChain) throws IOException, ServletException {
    request.setCharacterEncoding(encoding);
    filterChain.doFilter(request, response);
}

But it doesn't change anything. My ISO-8859-1 encoded String contains unexpected characters when it lands in the servlet.


Solution

  • The ServletRequest#setCharacterEncoding(String) method only sets the encoding used for the body of the request. There is no method you can use from the Servlet API to declare an encoding for the request URI. This is servlet container specific. With Tomcat, you need to do it at the <Connector> level.

    There's always the useBodyEncodingForURI config element you can use in <Connector> for setCharacterEncoding to do what you want. See here.