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.