0

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.

Mark Tielemans
  • 1,528
  • 3
  • 20
  • 40

1 Answers1

1

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.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for the insight. However, I suppose the decoding is performed somewhere by tomcat. If I could find where, perhaps I could override that somehow? – Mark Tielemans Aug 26 '14 at 16:55
  • @MarkTielemans That would require cloning all of Tomcat and re-writing/deploying your own version. There's nothing else, external, you can do. – Sotirios Delimanolis Aug 26 '14 at 17:38
  • Alright, thanks anyway! It's a shame applications cannot decide for themselves what encoding to use, in case several applications run on one environment, or an application doesn't always know what tomcat instance it is deployed to. – Mark Tielemans Aug 26 '14 at 18:09