Struts utf 8 and form submissions

Aug 2005

Struts will convert data that is received from form submissions to a default encoding because most browsers don’t set the content type.

To stop struts mangling the data you need to set the content type with a filter before it gets as far as the struts layer.

Use the filter below

package filters;

import javax.servlet.*;

public class UTF8Filter implements Filter
  public void destroy() {}

	public void doFilter(ServletRequest request,
		ServletResponse response, FilterChain chain)
		throws IOException, ServletException
		chain.doFilter(request, response);

	public void init(FilterConfig filterConfig)
		throws ServletException

You’ll need to add these settings to your web.xml


Be careful how you view the output of any form submissions, for instance the console on eclipse is not UTF-8 by default.