Solution for garbled JSP Chinese characters display, jsp garbled

Source: Internet
Author: User

Solution for garbled JSP Chinese characters display, jsp garbled

Solution to JSP Chinese display garbled Problem

I. garbled characters on the JSP page

2. garbled characters appear 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 server uses different encoding methods and browsers.

Result of different display results for different characters. Solution: Specify the encoding method (gb2312) on the JSP page, that is, the first

Add <% @ page contentType = "text/html; charset = gb2312" %> to remove 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">

<P align = "center">

<Input type = "text" name = "name">

<Input type = "submit" name = "Submit" value = "Submit">

</P>

</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: the browser uses UTF by default.

-8 encoding is used to send requests, while UTF-8 and GB2312 encoding are different in character encoding, which leads to unrecognized characters.

Solution: request. seCharacterEncoding ("gb2312") is used to encode the request in a unified manner.

Display. 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 characters are garbled in the varchar and text types. For the varchar type, set it to the binary attribute.

It can solve the Chinese problem. For the text type, an encoding and conversion class should 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 the structures in jsp are modified.

If no encoding is specified, the encoding of these strings is.

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

If it is B, the String is encoded as B rather than A, and is not the default value.

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 into A correct one.

It is A String encoded with A and then output.

2. In jsp, <% @ page contentType = "text/html; charset = A" %> is not specified, it is equivalent to specifying <% @

Page contentType = "text/html; charset = ISO-8859-1" %>

3. If response. setContentType ("text/html; charset = A") is executed in Servelte

Character output stream encoding is set to A. All String codes to be output must be converted to A. Otherwise, garbled characters are obtained.

The String code obtained from the request in Servelet is the same as that in jsp, but

String is the default encoding of the system. The String obtained from the external in the servelt uses the original encoding, such

The data obtained from A database whose code is B is encoded as B, not A, or the default encoding of the system.

//////////////////////////////////////// //////////////////////////////////////// //////////

Reprinted: JSP Chinese garbled problem solution Summary

In the process of using JSP, Chinese garbled characters are the biggest headache. The following are some of the problems I encountered in software development.

Code problem and solution.

1. garbled JSP pages

The reason for this Garbled text is that the character set encoding is not specified on the page. Solution: Use the following code at the beginning of the page.

You can specify the character set encoding in the area code,

2. Database garbled characters

This Garbled text will make Chinese characters you inserted into the database Garbled text or Garbled text when reading the display. The solution is as follows:

Add the encoding character set to the database connection string

String Url = "jdbc: mysql: // localhost/digitgulf?

User = root & password = root & useUnicode = true & characterEncoding = GB2312 ";

Use the following code on the page:

Response. setContentType ("text/html; charset = gb2312 ");

Request. setCharacterEncoding ("gb2312 ");

3. garbled characters are transmitted as parameters in Chinese.

When we pass a Chinese character as a parameter to another page, garbled characters also occur. The solution is as follows:

Encode parameters when passing parameters, such

RearshRes. jsp? Keywords = "+ java.net. URLEncoder. encode (keywords)

Then, use the following statement on the receiving parameters page to receive

Keywords = new String (request. getParameter ("keywords"). getBytes ("8859_1 "));

4. garbled JSP pages

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL .*"

ErrorPage = "err. jsp" %>

//////////////////////////////////////// //////////////////////////////////////// /////////

JSP/JDBC MySQL garbled problem ~~~

Author: workshop name Source: This site organized Release Date: 12:24:30

Starting from:

JSP requests are ISO8859_1 by default, so when processing Chinese,

To display Chinese characters, it must be converted to GBK, as shown below:

String str = new String (request. getParameter ("name"). getBytes ("ISO8859-1"), "GBK ");

Out. println (str );

This will display Chinese characters.

Chinese problems during MYSQL operations:

This depends on the default MySQL encoding. If it is not adjusted, latin1 is actually the same as ISO8859_1, so it must be processed during operations.

Same as him, otherwise it will be garbled

1. Insert Chinese characters:

String sql2 = "insert into test (name) VALUES ('" + request. getParameter ("name") + "')";

Stmt.exe cuteUpdate (sql2 );

You can insert it without coding.

2. display the inserted Chinese characters:

Because latin is saved, GBK is required for display.

String x = new String (rs. getString ("title"). getBytes ("ISO8859_1"), "GBK ");

Out. println (x );

3. Set the storage encoding:

Of course, when MySQL is latin1 encoded, it can also be stored with GBK.

Connection con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/jsp?

UseUnicode = true & characterEncoding = GBK "," root ","");

Str1 = "Chinese ";

String sql2 = "insert into test (name) VALUES ('" + str1 + "')";

This can also be inserted successfully.

//////////////////////////////////////// //////////////////////////////////////// ////////

Question about Chinese character encoding in JSP/Servlet

(Author: Zhang jianfang,

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.