Thursday, April 26, 2007

Tomcat UTF-8 CharsetMapper

Just a quickie I thought I'd share, if you're running into encoding problems using Tomcat and JSTL's fmt:xxx tags, you might want to implement your own Tomcat CharsetMapper. The following example will fix things for UTF-8 encoding:


public class UTF8CharsetMapper extends CharsetMapper {
public UTF8CharsetMapper() {
super();
}

public UTF8CharsetMapper(String name) {
super(name);
}

public String getCharset(Locale locale) {
return "UTF-8";
}
}


You should then edit your Tomcat's server.xml file by mapping your Context's charsetMapperClass attribute to your custom made CharsetMapper:


<Context charsetMapperClass="your.package.here.UTF8CharsetMapper" otherParam="xxx">
...
</Context>

No comments: