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.
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.
Thank you very much!
Your tip really saved my day!
All the best,
Armin
Great tip…
Keep up the good work…and thanks for saving me many hours of research..
Lance
thanks a lot for code..
I tried it for a long time
Once again thank u .
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!
Thanks!!!!!!!!.
Shouldnt the necoding be UTF-8 instead of UTF8.
From : request.setCharacterEncoding(”UTF8″);
To: request.setCharacterEncoding(”UT-F8″);
Sorry it should be :
From : request.setCharacterEncoding(â€UTF8″);
To: request.setCharacterEncoding(â€UTF-8″);
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.
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 ?
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 …
Hi
I do not know where to put or set Filter file in my ocde .. please provide me the detail of that.
I can only subscribe to the postings made above. Brilliant solution! Thank you
Thanks yaar !!!
The solution given is very simple and efficient
Thanks so much!!!
Brilliant solution!!!
Thank you very much!
Clear and Simple — And worked right away!
Thanks a lot for this help. I spent two weeks looking for a solution.
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.
Thanks alot, it save alot of time for my me
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.