4 solutions for page garbled

Source: Internet
Author: User

The following provides four garbled solutions for different situations, including the different situations where garbled codes occur in web development.
It mainly targets the garbled code solution proposed by the java + MySQL (the best combination with PHP) development environment. It can be used to solve the garbled problem in different language environments as long as it is modified accordingly.

I. garbled characters on the jsp (preferred for SUN Enterprise Applications) Page
The following display page (display. jsp (preferred for SUN Enterprise Applications) is garbled:

Program code
<Html>
<Head>
<Title> Chinese processing of jsp (preferred for SUN Enterprise Applications) </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<%
Out. print ("jsp (preferred for SUN Enterprise Applications ");
%>
</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: On the jsp (preferred for SUN Enterprise Applications) page, specify the encoding method (gb2312), that is, add the following to the first line of the page: <% @ 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> Chinese processing of jsp (preferred for SUN Enterprise Applications) </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<%
Out. print ("jsp (preferred for SUN Enterprise Applications ");
%>
</Body>
</Html>

2. garbled characters appear when the form is submitted to Chinese
The following is a submission page (submit. jsp (preferred for SUN Enterprise Applications). The Code is as follows:

Program code
<Html>
<Head>
<Title> Chinese processing of jsp (preferred for SUN Enterprise Applications) </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>
<Form name = "form1" method = "post" action = "process. jsp (preferred for SUN Enterprise Applications)">
<Div align = "center">
<Input type = "text" name = "name">
<Input type = "submit" name = "Submit" value = "Submit">
</Div>
</Form>
</Body>
</Html>

The following is the processing page (process. jsp (preferred for SUN Enterprise Applications) code:

Program code
<% @ Page contentType = "text/html; charset = gb2312" %>
<Html>
<Head>
<Title> Chinese processing of jsp (preferred for SUN Enterprise Applications) </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 (preferred for SUN Enterprise Applications) are correctly displayed, garbled characters will appear when you submit the 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 (preferred for SUN Enterprise Applications) code is as follows:

Program code
<% @ Page contentType = "text/html; charset = gb2312" %>
<%
Request. seCharacterEncoding ("gb2312 ");
%>
<Html>
<Head>
<Title> Chinese processing of jsp (preferred for SUN Enterprise Applications) </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.

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.