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.
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.
Link | April 27th, 2006 at 8:19 pm
Anonymous
Thank you very much!
Your tip really saved my day!
All the best,
Armin
Link | July 27th, 2006 at 4:31 am
Anonymous
Great tip…
Keep up the good work…and thanks for saving me many hours of research..
Lance
Link | August 2nd, 2006 at 1:17 pm
Anonymous
thanks a lot for code..
I tried it for a long time
Once again thank u .
Link | September 29th, 2006 at 10:29 am
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!
Link | October 21st, 2006 at 4:32 pm
Derek
Thanks!!!!!!!!.
Link | December 20th, 2006 at 7:58 am
Rajesh
Shouldnt the necoding be UTF-8 instead of UTF8.
From : request.setCharacterEncoding(”UTF8″);
To: request.setCharacterEncoding(”UT-F8″);
Link | February 15th, 2007 at 11:37 am
Rajesh
Sorry it should be :
From : request.setCharacterEncoding(â€UTF8″);
To: request.setCharacterEncoding(â€UTF-8″);
Link | February 15th, 2007 at 11:38 am
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.
Link | April 11th, 2007 at 8:55 pm
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 ?
Link | July 18th, 2007 at 11:40 am
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 …
Link | November 21st, 2007 at 4:33 am
Rahul
Hi
I do not know where to put or set Filter file in my ocde .. please provide me the detail of that.
Link | December 18th, 2007 at 1:25 am
Tom
I can only subscribe to the postings made above. Brilliant solution! Thank you
Link | January 9th, 2008 at 4:58 am
Gugga
Thanks yaar !!!
The solution given is very simple and efficient
Link | January 25th, 2008 at 1:18 am
Eugen G.
Thanks so much!!!
Brilliant solution!!!
Link | February 12th, 2008 at 9:27 am
Hotblack
Thank you very much!
Clear and Simple — And worked right away!
Link | September 7th, 2008 at 1:39 pm
Mohammad
Thanks a lot for this help. I spent two weeks looking for a solution.
Link | November 3rd, 2008 at 9:10 am