Home > Database > Mysql Tutorial > How to solve the garbled problem of MySQL and JSP

How to solve the garbled problem of MySQL and JSP

王林
Release: 2023-05-26 22:59:19
forward
911 people have browsed it

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:

  1. Modify the MySQL configuration file my.cnf and add the following statement under [mysqld]:

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

  1. 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:

  1. Add the following code to the head of the JSP page:

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

  1. In the form Set the character encoding to UTF-8:

  1. in the Tomcat configuration file Add the following code to server.xml:

       connectionTimeout="20000"
       redirectPort="8443"
       URIEncoding="UTF-8"/>

The above three methods are settings The character encoding of the JSP page is UTF-8 to avoid garbled characters when submitting data in the form.

The above is the detailed content of How to solve the garbled problem of MySQL and JSP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template