Jsp garbled problem collection

Source: Internet
Author: User

Recently, I was working on a small e-newspaper system and defined a String articleTitle variable. jsp jumps to result. jsp reads the variable normally, and then the result. jsp jumps to its own reading is not normal, delayed for a long time, and finally use session to solve. The following garbled processing seems to be only effective for simple pages...

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">
<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: 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 {

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.