I am going mental, international characters that I enter via a form are not being stored exactly as entered and the data stored is not being returned as its stored in the database.
If I enter 'çanak çömlek patladı' and click save on the form I use the page displays 'çanak çömlek patladı' but the database has stored 'çanak çömlek patlad? if I revisit the page again I get 'anak �mlek patlad?'' if I click save on the form without changing anything the database stores '?anak ??mlek patlad?' and the browser displays '?anak ??mlek patlad?'
I have my MySQL Server with the following config:
default-collation=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
default-character-set=utf8
The database character set is utf8 and database collation is utf8_unicode_ci and the table I am using is set as the same.
The first line of my JSP file is:
The html header is as so:
TestI have an EncodingFilter class compiled which is:
import java.io.IOException;
import javax.servlet.*;
public class EncodingFilter
implements Filter
{
public EncodingFilter()
{
}
public void init(FilterConfig filterconfig)
throws ServletException
{
filterConfig = filterconfig;
encoding = filterConfig.getInitParameter("encoding");
}
public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
throws IOException, ServletException
{
servletrequest.setCharacterEncoding(encoding);
filterchain.doFilter(servletrequest, servletresponse);
}
public void destroy()
{
}
private String encoding;
private FilterConfig filterConfig;
}
This class is referenced in my web.xml file as follows:
EncodingFilter
EncodingFilter
encoding
UTF-8
EncodingFilter
/*
I’ve restarted my system and therefore the Tomcat and MySQL server, checked the logs and there are no errors with any of the above configuration.
Can anyone please help, otherwise I'll have no hair left?
解决方案
Solved it, I ditched the previous db java class and wrote a new db function as it appeared that the previous developed class was causing a double encoding issue.
The error I was getting re the manual entering of 'çanak çömlek patladı' direct to the database related to an issue with MySQL not truly handing UTF-8 on varchar fields. As soon as I updated the field to varbinary, everything worked.
I hope this helps someone, I'm sure my hair will grow back, thank you to everyone who offered suggestions.
博主遇到一个字符编码问题,当输入包含国际字符的数据时,数据库存储和显示不一致。经过排查,发现是由于数据库varchar字段的问题。将字段类型改为varbinary后,问题得到解决。博客中提到了数据库配置、JSP文件头设置、过滤器配置以及最终的解决方案。

被折叠的 条评论
为什么被折叠?



