Ian Purton

Ian Purton’s Notes

Struts, UTF-8 and form submissions

  • PublishedFebruary 23rd, 2006
  • Authorian.purton
  • CategoriesJava

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.

"Struts, UTF-8 and form submissions" was published on February 23rd, 2006 and is listed in Java.

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Struts, UTF-8 and form submissions: 17 Comments

  1. Anonymous

    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

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

  3. Anonymous

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

    Lance

  4. Anonymous

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

  5. Ed

    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

    Thanks!!!!!!!!.

  7. Rajesh

    Shouldnt the necoding be UTF-8 instead of UTF8.

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

  8. Rajesh

    Sorry it should be :

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

  9. Roberto

    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

    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

    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

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

  13. Tom

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

  14. Gugga

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

  15. Eugen G.

    Thanks so much!!!
    Brilliant solution!!!

  16. Hotblack

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

  17. Mohammad

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

Leave Your Comment

(required)
(required)
 

Subtraction Wordpress Theme by Ian Purton