jsp mysql utf8,JSP,MySQL和UTF-8

博主遇到一个字符编码问题,当输入包含国际字符的数据时,数据库存储和显示不一致。经过排查,发现是由于数据库varchar字段的问题。将字段类型改为varbinary后,问题得到解决。博客中提到了数据库配置、JSP文件头设置、过滤器配置以及最终的解决方案。

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:

Test

I 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值