Struts, UTF-8 and form submissions

by ian.purton

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

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.

This entry was posted on Thursday, February 23rd, 2006 at 6:58 am and is filed under Java. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • Fiona
    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.
  • Do Van Vung
    Thanks alot, it save alot of time for my me
  • 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.
  • Mohammad
    Thanks a lot for this help. I spent two weeks looking for a solution.
  • Hotblack
    Thank you very much!
    Clear and Simple -- And worked right away!
  • Eugen G.
    Thanks so much!!!
    Brilliant solution!!!
  • Gugga
    Thanks yaar !!!
    The solution given is very simple and efficient
  • Tom
    I can only subscribe to the postings made above. Brilliant solution! Thank you
  • Rahul
    Hi
    I do not know where to put or set Filter file in my ocde .. please provide me the detail of that.
  • 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 ...
  • 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 ?
  • 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.
  • Rajesh
    Sorry it should be :

    From : request.setCharacterEncoding(”UTF8″);
    To: request.setCharacterEncoding(”UTF-8″);
  • Rajesh
    Shouldnt the necoding be UTF-8 instead of UTF8.

    From : request.setCharacterEncoding("UTF8");
    To: request.setCharacterEncoding("UT-F8");
  • Derek
    Thanks!!!!!!!!.
  • 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!
  • Anonymous
    thanks a lot for code..
    I tried it for a long time
    Once again thank u .
  • Anonymous
    Great tip...
    Keep up the good work...and thanks for saving me many hours of research..

    Lance
  • Anonymous
    Thank you very much!
    Your tip really saved my day!
    All the best,
    Armin
  • 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.
blog comments powered by Disqus