Search code examples
javaweb-applicationsservletsutf-8url-encoding

Configuring Tomcat 5.5 to UTF-8 encode all sendRedirect() redirections?


A requirement of the product that we are building is that its URL endpoints are semantically meaningful to users in their native language. This means that we need UTF-8 encoded URLs to support every alphabet under the sun.

We would also not like to have to provide installation configuration documentation for every application server and version that we support, so it would be nice if we could accomplish this in-code. This might not be possible, since by the time that the Servlet has received the request, its been encoded by the App server, etc.

I've gotten this working (for my first use case using ISO-Latin non-US ASCII characters) by reconstituting the request's path info with:

String pathInfoEncoded = new String(httpServletRequest.getPathInfo().getBytes(), "UTF-8");

and then parsing that.

However, this doesn't work after redirecting from a POST to a GET using sendRedirect(). The request's path comes in already escaped (so ö is encoded as %F6) and my method above doesn't work.

So I guess my question is am I going about this all wrong? And if so, whats the antidote to my ignorance? :)

Update : found the solution. The problem is that the Servlet API has some weird behaviour with regards to URL encoding before sending the redirect. You have to URL-encode (escape the UTF-8 characters) BEFORE you call sendRedirect(). The encodeRedirectURL() method doesn't do it for you.

This page discusses it: http://www.whirlycott.com/phil/2005/05/11/building-j2ee-web-applications-with-utf-8-support/


Solution

  • found the solution. The problem is that the Servlet API has some weird behaviour with regards to URL encoding before sending the redirect. You have to URL-encode (escape the UTF-8 characters) BEFORE you call sendRedirect(). The encodeRedirectURL() method doesn't do it for you.

    This page discusses it: http://www.whirlycott.com/phil/2005/05/11/building-j2ee-web-applications-with-utf-8-support/