Jetty: Utf8Appendable$NotUtf8Exception: Not valid UTF8!

Configuring a webserver can be tough. Especially if you have a ten year old, legacy and grown system to consider.

I've recently updated a Jetty from version 5 to a current relase (9.x). Being currently in the staging phase, I stumble upon various problems. One of these was the occurence of the following warning message:

WARN:oeju.UrlEncoded:qtp1053631449-45: org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte Fc in state 0
WARN:oeju.UrlEncoded:qtp1053631449-45: org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! char appended in state 36

After some debugging, I found the problem. As I said, the system is quite grown, there is still a lot of ISO-8859-1 ongoing. Jetty assumes UTF-8. So I had to configure Jetty appropriately.

Parsing a request's parameter

The class org.eclipse.jetty.server.Request has a method called setQueryEncoding. Calling this method, one can define the way how Jetty parses the URL parameters. Exactly this lead to the warning message and an incorrect behaviour of the application. Because the issue was obviously more complex (Some servlets served UTF-8) I installed a filter.

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
   if(req instanceof Request){
      Request jReq = ((Request)req);
      jReq.setQueryEncoding("ISO-8859-1");
   }
   chain.doFilter(req, res);
}

Leave a Comment

comments powered by Disqus