At the beginning of self-study JSP, in the course of the practice encountered a very serious problem, is the Chinese characters garbled problem, I used three days of time, collect information, finally solved the problem, now to learn something to carry out a summary finishing.
1. The first is the JSP page display garbled problem,<title></title> tag in Chinese has English, set <meta charset= "Utf-8" > Display garbled, changed to GBK normal display, Other pages using Utf-8 are displayed as normal. The final discovery is because the file is created using a different method, in general, write code in Notepad, save the file as *.html, saving type Select all the files, encoding select UTF-8, then the file suffix name to. JSP, this is the most secure.
Reference http://www.blogjava.net/luedipiaofeng/articles/307666.html
In the JSP file header should be written like this:
<%@ page language= "java" pageencoding= "UTF-8"%> jsp file storage format
<%@ page contenttype= "Text/html;charset=utf-8"%> decoding format, must be consistent with the storage format, otherwise <body></body> In the "Hello" will show garbled
<meta http-equiv= "Content-type" content= "text/html; charset=utf-8"> control how the browser is decoded
<body>
How are you doing
</body>
2. Now I have two files, a register.html and a register.jsp, to enter the registration information on the register.html page and pass it to the register.jsp file, which writes the received data to the database. There is a normal English writing, Chinese garbled situation. If the Chinese part is all??? , the Chinese characters failed to write to the database, and if the unreadable characters are garbled, it is only a problem to prove that the database is written in English.
When the Chinese part is full??? Or you cannot write to the database by using the INSERT statement, I re-created the database.
Create DATABASE Test DEFAULT CHARSET SET UTF8 COLLATE utf8_general_ci; (set default character set, and database validation rules)
Use test;
CREATE TABLE student (ID int auto_increment PRIMARY key NOT NULL, name varchar (TEN) not NULL, password varchar (a) NOT NULL ) Engine=innodb DEFAULT Charset=utf8 collate=utf8_general_ci;
Show create table student; Display information for creating tables
Show variables like ' character_set_% '; View the database character set
Reference: http://www.2cto.com/database/201406/306012.html
I found that when I import data from the HTML into the JSP file, is already garbled, this garbled is Tomcat's internal encoding format iso8859-1 in the trouble, that is, when the post commits, if not set the encoding format of the submission, it will be submitted in iso8859-1 Way , the (Tomcat default code: ISO8859-1) accepted JSP is accepted in a utf-8 manner. causes garbled characters.
There are two ways to solve this problem
1>. encoding conversion When parameters are accepted String name = new String (Request.getparameter ("uname"). GetBytes ("Iso-8859-1"), "Utf-8"); transcoding for a single parameter
2>. Add request.setcharacterencoding ("UTF-8") to the code before the JSP accepts the parameter . transcoding for all parameters
The parameters received by the JSP are finally displayed correctly.
Reference: http://blog.csdn.net/centre10/article/details/5903924
The problem of garbled characters of Chinese characters in JSP and MySQL