The problem of garbled characters in MySQL and JSP is a common problem in web development. If this problem is not solved, garbled characters will appear after the application is deployed and affect the normal operation of the program. This article will introduce the causes of garbled characters in MySQL and JSP and how to solve them.
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 encoding to meet the needs of most applications. In MySQL, we can set the character set through the following steps:
[ mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
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 characters of MySQL Set to UTF-8 to avoid 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:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>