JSP Chinese problems

Source: Internet
Author: User

I. garbled characters are displayed on the JSP page. II. garbled characters occur when the form is submitted to Chinese. III. Database Connection
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>

Pair
Different web servers and different 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>

For example
If the English characters submitted by submit. jsp are correctly displayed, garbled characters will appear when you submit Chinese characters. Cause: the browser uses the UTF-8 encoding method by default to send requests, while the UTF-8 and
The gb2312 encoding method indicates different characters, so that the characters cannot be recognized. Solution: request. setcharacterencoding
("Gb2312") requests are encoded in a unified manner 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.

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.