JSP,MySQL和UTF-8 [英] JSP, MySQL and UTF-8

查看:72
本文介绍了JSP,MySQL和UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在思考,我通过表单输入的国际字符不会完全按照输入的方式存储,并且存储的数据也不会像存储在数据库中那样返回.

如果我输入çanakçömlekpatladı"并单击保存,那么我使用的页面将显示çanakçömlekpatladı",但数据库是否存储了çanakçömlekpatlad"?如果再次访问该页面,则会显示￧anakmlmlek patlad?",如果我单击表单上的保存"而不更改任何内容,数据库将存储"anak ?? mlek patlad?".然后浏览器显示?anak ?? mlek patlad?"

我的MySQL服务器具有以下配置:

default-collation=utf8
collation_server=utf8_unicode_ci
character_set_server=utf8
default-character-set=utf8

数据库字符集为utf8,数据库排序规则为utf8_unicode_ci,并且我使用的表设置为相同.

我的JSP文件的第一行是:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

html标头是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>

我编译了一个EncodingFilter类,该类是:

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;
}

在我的web.xml文件中引用了此类,如下所示:

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

我已经重新启动系统,因此重新启动了Tomcat和MySQL服务器,检查了日志,以上任何配置都没有错误.

任何人都可以帮忙,否则我没有头发了吗?

解决方案

解决了这个问题,我放弃了以前的db java类,并编写了一个新的db函数,因为以前的开发类似乎引起了双重编码问题.

我得到的错误是直接向数据库手动输入çanakçömlekpatladı",这与MySQL的问题有关,而该问题并未真正将utchar-8传递给varchar字段.我将字段更新为varbinary后,一切正常.

我希望这对某人有帮助,我确定我的头发会长回来,谢谢所有提出建议的人.

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:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

The html header is as so:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>

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:

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

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.

这篇关于JSP,MySQL和UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆