JSP + MySQL Chinese garbled problem post submit garbled Solution

Source: Internet
Author: User

Two jsp pages are written: index. jsp and mysql_insert.jsp. The data processing process is as follows: access index on the browser (chrome. jsp then enters data in the form and submits the data to mysql_insert.jsp. mysql_insert.jsp first saves the received data to the person_tb of the html_db database of MySQL (the original part of the table data) according to the variable ), then, mysql_insert.jsp displays all the data in the table on the mysql_insert.jsp page.

It is found that when the submitted data contains Chinese characters (for example, the variable name value is Chinese), the corresponding Chinese characters (name value) in the newly added record are garbled on the mysql_insert.jsp page, other data is displayed normally. Check the database and find that the corresponding variable value (name value) containing Chinese characters is garbled. Garbled information such:
 
Index. the first line in jsp contains a statement: <% @ page contentType = "text/html; charset = gb2312" %> in the browser (chrome) there is no Garbled text when accessing this page. (It mainly refers to Chinese Garbled text and English Garbled text ). (Change gb2312 to UTF-8 and garbled Chinese characters After access)
The first line in mysql_insert.jsp contains the statement <% @ page language = "java" pageEncoding = "UTF-8" %>, which is not garbled when accessing this page directly in a browser.
The code for the mysql_insert.jsp page is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. *" %>
<HTML>
<HEAD>
<TITLE> add message into table </TITLE>
</HEAD>
<BODY>
<%
String id = request. getParameter ("id"); // obtain from the form
String name = request. getParameter ("name"); // obtain from the form
String sex = request. getParameter ("sex"); // obtain from the form
String age = request. getParameter ("age"); // obtain from the form
Try
{
/** Database connection parameters **/
String driverName = "com. mysql. jdbc. Driver"; // Driver name
String DBUser = "root"; // mysql user name
String DBPasswd = "123456"; // mysql password
String DBName = "html_db"; // Database Name

String connUrl = "jdbc: mysql: // localhost/" + DBName + "? User = "+ DBUser +" & password = "+ DBPasswd;
Class. forName (driverName). newInstance ();
Connection conn = DriverManager. getConnection (connUrl );
Statement stmt = conn. createStatement ();
Stmt.exe cuteQuery ("set names UTF8 ");
String insert_ SQL = "insert into person_tb values ('" + id + "', '" + name + "', '" + sex + "', '"+ age + "')";
String query_ SQL = "select * from person_tb ";

Try {
Stmt.exe cute (insert_ SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
ResultSet rs = stmt.exe cuteQuery (query_ SQL );
While (rs. next ()){
%>
ID: <% = rs. getString ("id") %> </br>
Name: <% = rs. getString ("name") %> </br>
Gender: <% = rs. getString ("sex") %> </br>
Age: <% = rs. getString ("age") %> </br>
<%
}
} Catch (Exception e ){
E. printStackTrace ();
}
// Rs. close ();
Stmt. close ();
Conn. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
%>
</Body>
</Html>

My database sets all to use UTF-8 encoding, such:
 
The web. xml content in my virtual directory is as follows:
 
The contents of the server. xml file in the tomcat/conf directory are as follows:
Copy codeThe Code is as follows:
<U> <? Xml version = '1. 0' encoding = 'utf-8'?> </U>
<Server port = "8005" shutdown = "SHUTDOWN">
<Listener className = "org. apache. catalina. core. AprLifecycleListener" SSLEngine = "on"/>
<Listener className = "org. apache. catalina. core. JasperListener"/>
<Listener className = "org. apache. catalina. core. JreMemoryLeakPreventionListener"/>
<Listener className = "org. apache. catalina. mbeans. GlobalResourcesLifecycleListener"/>
<Listener className = "org. apache. catalina. core. ThreadLocalLeakPreventionListener"/>
<GlobalNamingResources>
<Resource name = "UserDatabase" auth = "Container"
Type = "org. apache. catalina. UserDatabase"
Description = "User database that can be updated and saved"
Factory = "org. apache. catalina. users. MemoryUserDatabaseFactory"
Pathname = "conf/tomcat-users.xml"/>
</GlobalNamingResources>
<Service name = "Catalina">
<Connector port = "8080" protocol = "HTTP/1.1"
ConnectionTimeout = "20000"
RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>
<Connector port = "8009" protocol = "AJP/1.3" redirectPort = "8443"/>

<Engine name = "Catalina" defaultHost = "localhost">
<Realm className = "org. apache. catalina. realm. LockOutRealm">
<Realm className = "org. apache. catalina. realm. UserDatabaseRealm"
ResourceName = "UserDatabase"/>
</Realm>
<Host name = "localhost" appBase = "webapps" unpackWARs = "true" autoDeploy = "true">
<Valve className = "org. apache. catalina. valves. AccessLogValve" directory = "logs"
Prefix = "localhost_access_log." suffix = ". txt"
Pattern = "% h % l % u % t" % r "% s % B"/>
</Host>
</Engine>
</Service>
</Server>

The main content of the web. xml file in the tomcat/conf directory is as follows:
Copy codeThe Code is as follows:
<U> <? Xml version = "1.0" encoding = "UTF-8"?> </U>
<Web-app xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
Version = "3.0">
<Servlet>
<Servlet-name> default </servlet-name>
<Servlet-class> org. apache. catalina. servlets. DefaultServlet </servlet-class>
<Init-param>
<Param-name> debug </param-name>
<Param-value> 0 </param-value>
</Init-param>
<Init-param>
<Param-name> listings </param-name>
<Param-value> false </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-name> jsp </servlet-name>
<Servlet-class> org. apache. jasper. servlet. JspServlet </servlet-class>
<Init-param>
<Param-name> fork </param-name>
<Param-value> false </param-value>
</Init-param>
<Init-param>
<Param-name> xpoweredBy </param-name>
<Param-value> false </param-value>
</Init-param>
<Load-on-startup> 3 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-name> default </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>
<! -- The mappings for the JSP servlet -->
<Servlet-mapping>
<Servlet-name> jsp </servlet-name>
<Url-pattern> *. jsp </url-pattern>
<Url-pattern> *. jspx </url-pattern>
</Servlet-mapping>
<Session-config>
<Session-timeout> 30 </session-timeout>
</Session-config>
<The content of mime-mapping is omitted here>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index.htm </welcome-file>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

Now I can only think of so many encoding settings. Where else do I need to set encoding? Beg for guidance.
Hahaha, after some hard exploration, I finally found the cause of the garbled code. Of course, the garbled code problem was also solved. Brother-in-law recommended that all codes use UTF-8, so I also recommend that readers do the same, a lot of benefits.
Next I will introduce my solution process:
Step 1: After I find garbled characters, the first thing I think is whether the encoding I set is incorrect somewhere, and the settings in MysQL are correct (my. ini), that is, all are set to utf8. Therefore, I do not need to modify the settings in MySQL.
Step 2: Then tomcat. This requires processing of the data submitted by me. Many people on the Internet have suggested setting up the web. xml and server. the encoding in xml, mainly in server. add URIEncoding = "UTF-8" in xml, specifically set: <Connector port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443" URIEncoding = "UTF-8"/>. After I complete this step, I tested it again and found that it was still garbled. Keep the modifications in this step and proceed to the next step.
Step 3: return. jsp page, I tried to set all the places involved in encoding and character sets to UTF-8 (I previously in index. the first line in jsp is <% @ page language = "java" contentType = "text/html; charset = gb2312" %>. use dreamweaver to open the index. jsp modifies it to charset = UTF-8), and then directly accesses the index in the browser. jsp found Chinese garbled characters, so close dreamweaver and use myeclipse to re-open index. in the jsp file, the Chinese characters in the Code are garbled and are modified to the correct Chinese characters. Besides, the modified charset is retained, that is, charset = UTF-8 is still used, and then the index is accessed in the browser. jsp, and the Chinese characters are displayed normally. (Later I said goodbye to dreamweaver, And I spared a lot of trouble) Then I entered Chinese data on the index. jsp page and submitted the data. Chinese garbled characters remain. After this step is modified, the headers of my two. jsp files have the following two sentences:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>

Step 4: I was just thinking about whether the garbled characters caused by data inventory or the garbled characters caused by data retrieval from the database? Or is the garbage code generated when the data is transmitted to the mysql_insert.jsp page after submission? Then I directly inserted a piece of data containing Chinese characters in the database, and then directly accessed mysql_insert.jsp in the browser, and found that Chinese characters can be properly displayed. That is to say, it is garbled during data storage or data transmission. So how can we avoid data transmission and data storage without garbled characters?
Step 5: Find the following content online:
1. You need to set the encoding method of the jsp page in two ways:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>

Here, pageEncoding refers to the encoding method when the jsp file is stored locally. The charset of contentType refers to the encoding used when the server sends the webpage content to the client.
From the first visit to a jsp page and the page is sent to the client, the Jsp page must undergo three encoding conversions:
The first stage is jsp compilation. java, it will read jsp according to pageEncoding settings, the result is translated by the specified encoding scheme into a unified UTF-8 JAVA source code (that is. java). If pageEncoding is set incorrectly or is not set, Chinese garbled characters are displayed.
The second stage is the JAVA source code of JAVAC to the compilation of java byteCode, No matter what encoding scheme is used in JSP writing, after this stage the results are all the java source code of the UTF-8 encoding.
JAVAC uses the UTF-8's encoding to read the java source code and compile it into the UTF-8's encoding binary code (that is,. class), which is the JVM's specification for the constant string expression in the binary code (java encoding.
The third stage is the JAVA binary code loaded and executed by Tomcat (or its application iner INER) in Stage 2. The output result is displayed on the client, in this case, the parameter contentType hidden in phase 1 and phase 2 is effective.
The final solution is:
Set one of pageEncoding or contentType on the jsp page to support Chinese encoding formats (such as UTF-8, gbk, and gb2312 ). If you set one, the other will be the same as it by default.
If both are set, make sure that both support Chinese encoding (not necessarily the same ).
The Recommended settings are as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>

2. garbled values transmitted in post mode:
Because the post method stores values through request, the request is also used on another page. getParameter (String name) is used to extract information. In this case, garbled characters are mainly caused by the encoding settings of the request storage information. When a post is submitted, if no encoding format is set for submission, it is submitted in iso8859-1 mode, and the accepted jsp is accepted in UTF-8 mode. So use the following statement to obtain a single correct Chinese String: String str = new String (request. getParameter ("something"). getBytes ("ISO-8859-1"), "UTF-8 ");
Solution:
1. Set request. setCharacterEncoding ("UTF-8") on the receive page ").
2. It is best to set each page to request. setCharacterEncoding ("UTF-8") through a filter ").
3. On the send data page, use the statement to specify to send data in UTF-8 format.
3. garbled characters in get mode:
There are two methods to pass values through get, one is form get and the other is url address (in essence, both methods use url parameters to pass values ).
Get values in form mode:
The encoding process for get pass-through is as follows: first, the browser encodes the pass-through value based on the charset encoding method on the page, and then submits it to the server for tomcat. When tomcat decodes this information, the decoding method is implemented by the server. the URIEncoding settings in the xml file are determined, that is, when we use the request. the string obtained when getParameter ("") obtains the form parameter value, which is encoded by charset and decoded by URIEncoding.

As we know, as long as charset encoding is consistent with URIEncoding decoding and supports Chinese characters, no garbled characters can be found.
The method for setting URIEncoding is as follows:
Method 1:
Modify the $ TOMCAT/conf/server. xml file and add URIEncoding = "UTF-8" to the configuration of HTTP ctor or AJP Connector"
Copy codeThe Code is as follows:
<... MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true" URIEncoding = "UTF-8"/>

Method 2:
Use useBodyEncodingForURI = "true". This method is suitable for running multiple Encoding programs in your TOMCAT instance.
Copy codeThe Code is as follows:
<... MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
ConnectionTimeout = "20000" disableUploadTimeout = "true" useBodyEncodingForURI = "true"/>
EnableLookups = "false" redirectPort = "8443" protocol = "AJP/1.3" useBodyEncodingForURI = "true"/>

In Tomcat configuration, the Connector (HTTP Connector) attribute has a URIEncoding and
UseBodyEncodingForURI attribute. These two attributes set how to select character set encoding when URL Decoding is performed on additional parameters of the URL. URIEncoding is used to encode the character set of additional parameters after the URL. useBodyEncodingForURI indicates whether to use the character set encoding settings of the object content to replace the setting of URIEncoding. That is to say, when the useBodyEncodingForURI attribute is set. the character set encoding set by the setCharacterEncoding method also affects the URL Decoding result of getParameter and other methods on URL parameters. (Find the <Connector> mark in the/% TomCat_Home %/conf \ server. xml file, and add useBodyEncodingForURI = true to the end)
Garbled characters are transmitted using get in url mode:
In this way, the browser will not use the charset method of the page to encode the Chinese characters in the URL and then submit them to the server (IE and FireFox are all the same ), instead, the system uses GBK transcoding as the ISO-8859-1 and then submits it to the Server tomcat, so this process is:
First, the url address in the Chinese is converted from gbk to ISO-8859-1, handed to tomcat, and tomcat according to URLEcoding decoding, in this case, only the URLEcoding set to gbk in the request. getParameter ("") is not garbled. But this will affect the above configuration, so a good solution is to use java.net. URLEcoder and URLDecoder to manually encode and decode the Chinese characters in the address.
Therefore, the solution to the 10 thousand full solution is:
1) charset for all pages is set to UTF-8.
2) Tomcat's URIEncoding is the ISO-8859-1 by default, and I set to UTF-8, mainly want to solve the Chinese naming file and request to submit in get mode possible garbled problem.
3) Add a filter and call the request. setCharacterEncoding ("UTF-8") method to set the request character set to UTF-8. This solves the issue of garbled Code submitted in post mode.
4) When a Chinese parameter exists in a url, URLEcoder is first encoded as UTF-8 for the Chinese parameter, and then restored using URLDecoder after receiving the parameter in request. getParameter. For example:
From. jsp page:
Copy codeThe Code is as follows:
<% String username = "Zhang xx ";
Username = URLEncoder. encode (username, "UTF-8 ");
%>
<A href = "to. jsp? Param = <% = username %> "> transfer in </a>

To. jsp page
Copy codeThe Code is as follows:
<% = URLDecoder. decode (request. getParameter ("param"), "UTF-8") %>

In short, the garbled solution is as follows:
When the post value is garbled, Set request. setCharacterEncoding ("UTF-8") on the receiving end ")
When the get value or url is garbled, manually set the received parameter String str = new String (request. getParameter ("something"). getBytes ("ISO-8859-1"), "UTF-8 ");
The get and post values are different in Tomcat 5.
After reading the text in the red part above, I decided to set the page for submitting data in UTF-8 format while the page setting for receiving data also received data in UTF-8, so I added the following statements to the headers of both pages:
<%
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
%>

Then test, OK! No gibberish!
The code for the index. jsp page is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page contentType = "text/html; charset = UTF-8" %>
<%
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
%>
<Html>
<Head>
</Head>
<Body>
<Form action = "mysql_insert.jsp" method = "post">
ID: <input type = "text" name = "id" value = "0"/>
Name: <input type = "text" name = "name" value = "aaa"/>
Gender: <input type = "text" name = "sex" value = "female"/>
Age: <input type = "text" name = "age" value = "20"/>
</Br>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.