Home >Database >Mysql Tutorial >How to solve the garbled problem of MySQL and JSP
1. MySQL garbled code problem
The default character set used by MySQL is Latin1, but in most cases, we need to use the UTF-8 character set , because the UTF-8 character set supports multiple language text encodings and can meet the needs of most applications. In MySQL, we can set the character set through the following steps:
Modify the MySQL configuration file my.cnf and add the following statement under [mysqld]:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
Modify the MySQL default character set and execute the following command:
SET character_set_client=utf8;
SET character_set_connection=utf8;
SET character_set_database=utf8;
SET character_set_results=utf8;
SET character_set_server=utf8;
The above two methods are to modify the default character set of MySQL to UTF-8, thereby avoiding garbled characters when reading or writing data.
2. JSP garbled code problem
JSP garbled code problem most often occurs when the form submits data. This is because the data sent by the browser to the server is manually input by the user, and the character encoding may be a variety of possible encoding methods, including GB2312, Big5 and other encoding methods, resulting in garbled characters after the data is transmitted to the server.
The methods to solve the problem of JSP garbled characters generally include the following:
Add the following code to the head of the JSP page:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
In the form Set the character encoding to UTF-8: