February 23, 2006

Struts, UTF-8 and form submissions

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 java.io.IOException;
import javax.servlet.*;

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

	public void doFilter(ServletRequest request,
		ServletResponse response, FilterChain chain)
		throws IOException, ServletException
	{
		request.setCharacterEncoding("UTF8");
		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig)
		throws ServletException
	{
	}
}

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

<filter>
	<filter-name>UTF8Filter</filter-name>
	<filter-class>filters.UTF8Filter</filter-class>
</filter>
<filter-mapping>
	<filter-name>UTF8Filter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

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

20 Comments

  1. Anonymous says:

    Thanks for the tip!!! I was searching everywhere on the net and changing character encodings everywhere in the code but could not resolve the problem. Thanks for helping me with my problem.

  2. Anonymous says:

    Thank you very much!
    Your tip really saved my day!
    All the best,
    Armin

  3. Anonymous says:

    Great tip…
    Keep up the good work…and thanks for saving me many hours of research..

    Lance

  4. Anonymous says:

    thanks a lot for code..
    I tried it for a long time
    Once again thank u .

  5. Ed says:

    Also think it’s a brilliant tip – i spent ages trying to sort this out before i found this… couldn’t have done it without you!!!

    thanks!

  6. Derek says:

    Thanks!!!!!!!!.

  7. Rajesh says:

    Shouldnt the necoding be UTF-8 instead of UTF8.

    From : request.setCharacterEncoding(”UTF8″);
    To: request.setCharacterEncoding(”UT-F8″);

  8. Rajesh says:

    Sorry it should be :

    From : request.setCharacterEncoding(”UTF8″);
    To: request.setCharacterEncoding(”UTF-8″);

  9. Roberto says:

    For who it is in the development of the first project and finds a solution so practical. It is very good. Easy to implement, fantastic. Congratulations. Thank you.

  10. jy says:

    I know this solution and have ever improved it on one of my applications.
    But on another application, I have used the same solution but it doesn’t work :
    my filter is the first declared in my web.xml, but the code line : request.setCharacterEncoding(”ISO-8859-15″); seems to not work.
    any idea to help me ?

  11. delirii says:

    Your tip only works if coupled with a Tomcat connectors modification :
    I was obliged to add these attributes in order to get full (and effective) UTF-8 encoding :

    URIEncoding=”UTF-8″ useBodyEncodingForURI=”true”

    NOTE : it may help jy …

  12. Rahul says:

    Hi
    I do not know where to put or set Filter file in my ocde .. please provide me the detail of that.

  13. Tom says:

    I can only subscribe to the postings made above. Brilliant solution! Thank you

  14. Gugga says:

    Thanks yaar !!!
    The solution given is very simple and efficient

  15. Eugen G. says:

    Thanks so much!!!
    Brilliant solution!!!

  16. Hotblack says:

    Thank you very much!
    Clear and Simple — And worked right away!

  17. Mohammad says:

    Thanks a lot for this help. I spent two weeks looking for a solution.

  18. Tom Harrison says:

    Thanks for this post. In my case, I also had to set response.setCharacterEncoding(”UTF-8″); to get everything to work. Adding the filter class and the directives in web.xml is simple.

  19. Do Van Vung says:

    Thanks alot, it save alot of time for my me

  20. Fiona says:

    In my case I have to show Japanese character on the page and to be saved into database. I’ve tried with filter but it doesn’t work out although it’s going to filter class but the charachter are still in ?? format. Could someone help me? Thanks in advance.