JSP Chinese display Solution

Source: Internet
Author: User
I. garbled characters on the JSP page ii. garbled characters 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>
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 methods indicate different characters, which leads to unrecognized characters. 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, the solution is to add
Useunicode = true & characterencoding = GBK is OK.

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/

Summary:

1. in JSP, if <% @ page contenttype = "text/html; charset = A" %> is specified, all constructed strings (not references) in JSP are modified ), if no encoding is specified, these strings are encoded as.
The string obtained from the request is a iso-8859-1 if no request encoding is specified.
The string obtained from other places uses the original initial encoding. For example, the string obtained from the database. If the database encoding is B, the string encoding is B rather than, it is not the default one.
At this time, if the encoding of the string to be output is not a, it is likely to display garbled characters. Therefore, you must first convert the string to the string encoded as A and then output it.

2. in JSP, <% @ page contenttype = "text/html; charset = A" %> is not specified, so <% @ page contenttype = "text/html; charset = ISO-8859-1 "%>

3. in servelte, if response. setcontenttype ("text/html; charset = A"); specify the encoding of the response character output stream to A. encode all strings to be output to, otherwise, garbled characters are obtained.
The string obtained from the request in servelet is the same as that in JSP, but the string constructed in the servlet Java file is the default encoding of the system. In servelt, the string obtained from the outside uses the original encoding. For example, the data obtained from the database encoded as B is encoded as B, not, it is not the default 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.