Garbled characters are displayed on the JSP page. 2. garbled characters occur when the form is submitted to Chinese. 3. database connection.

Source: Internet
Author: User

An error occurred while submitting the Chinese word to the database during the exercise, so I found this article.

The change in MySQL. ini does not play a role. It cannot be solved if it is converted to gb2312.

Request. secharacterencoding ("gb2312 ");

You have not tried. Go home and try again.

Below is the post:

Chinese garbled characters often occur during JSP development, which may affect you, I am writing out the Chinese garbled characters I encountered during JSP development and the solutions for your reference.

I. garbled characters on the JSP page
The following display page (display. jsp) is garbled:
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<%
Out. Print ("JSP Chinese processing ");
%>
</Body>
</Html>
Different web servers and JDK versions have different processing results. Cause: the encoding method used by the server is different from that used by the browser to display different characters. Solution: Specify the encoding method (gb2312) on the JSP page, that is, add <% @ page contenttype = "text/html; charset = gb2312 "%> to eliminate garbled characters. The complete page is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<%
Out. Print ("JSP Chinese processing ");
%>
</Body>
</Html>

2. garbled characters appear when the form is submitted to Chinese
The following is a submission page (submit. jsp). The Code is as follows:
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<Form name = "form1" method = "Post" Action = "process. jsp">
<Div align = "center">
<Input type = "text" name = "name">
<Input type = "Submit" name = "Submit" value = "Submit">
</Div>
</Form>
</Body>
</Html>
The following is the process. JSP code:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<% = Request. getparameter ("name") %>
</Body>
</Html>
If the English characters submitted by submit. jsp are correctly displayed, garbled characters will appear when you submit Chinese characters. Cause: by default, the browser uses the UTF-8 encoding method to send requests, while the UTF-8 and gb2312 encoding method to indicate the character is not the same, so there is an unrecognized character. Solution: requests are uniformly encoded using request. secharacterencoding ("gb2312") to display Chinese characters normally. The modified process. JSP code is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
Request. secharacterencoding ("gb2312 ");
%>
<HTML>
<Head>
<Title> JSP Chinese processing </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<% = Request. getparameter ("name") %>
</Body>
</Html>

3. garbled database connection
As long as all Chinese characters are garbled, solution: Add useunicode = true & characterencoding = GBK to the Database URL.

4. garbled display of databases
In mysql4.1.0, Chinese garbled characters are displayed for the varchar and text types. Setting the varchar type as the Binary Attribute can solve Chinese problems, for the text type, an encoding and conversion class must be used for processing. The implementation is as follows:
Public class convert {
/** Convert the ISO-8859-1 code to gb2312
*/
Public static string isotogb (string ISO ){
String GB;
Try {
If (ISO. Equals ("") | ISO = NULL ){
Return "";
}
Else {
ISO = ISO. Trim ();
GB = new string (ISO. getbytes ("ISO-8859-1"), "gb2312 ");
Return GB;
}
}
Catch (exception e ){
System. Err. Print ("encoding conversion error:" + E. getmessage ());
Return "";
}
}
}
By compiling it into a class, you can call the static method isotogb () of the convert class to convert the encoding.

If you do not understand anything: I recommend a good JSP-JAVA site for everyone:

Http://www.phy.hbnu.edu.cn/dsp/

Linux and Java are the darling of the internet, and they are also the two masters in the Internet era. Linux's network security and open source code, Java's platform independence and security and ease of use are exactly in line with the requirements of the Internet platform, linux and Java are just a wonderful combination. The shuangshi Resources Network provides you with the best Linux and Java learning resources. You are welcome to visit and publicize the following:
Http://www.phy.hbnu.edu.cn/dsp/
 

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.