JSP, MySQL and UTF-8

2019-05-18 21:36发布

问题:

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?

回答1:

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.



回答2:

Before retrieving anything from database, execute SET NAMES UTF8 query and then all of the queries. You will then get the chars the way they look.



回答3:

You need to check following things :

  1. Your database should support multilingualism(If it's MySQL, at the time of installation itself it will ask your for this option).


回答4:

Start with http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8

Your character-holding columns in MySQL will have to be able to accept UTF-8 characters. Most MySQL defaults are for latin1 character sets unless your DDL scripts set a character set for a column.

If you want to change a table from one encoding to another, you can do it like this:

mysql> ALTER TABLE t CONCERT TO CHARACTER SET 'utf8'

I think you can also do this on a column-by-column basis if you really want to as well. Note that ALTER may take a looooong time on tables with a lot of data, lots of indexes, etc.



回答5:

When you connect to your database can you make sure you set "characterEncoding" as "utf8"? Also, if you open your pages in a browser (eg. ffox) what is the character encoding that is being displayed?

I think you should try a bit of "divide et impera". It is important to learn if MYSQL is the problem or not (I suspect not). To do this you can insert the UTF String in your application and save it to the database. If this is not working we know the problem is mysql. If it works it is probably the servlet.

It wouldn't hurt to add this at the servlet level:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");


回答6:

I have got character encoding problems(JSP-Mysql).I wrote String URL = "jdbc:mysql://localhost/deneme?useUnicode=true&characterEncoding=ISO-8859-1"; and solved the problem. But, database's table must UTF-8.