Solve the problem of Chinese garbled notes in JSP

Source: Internet
Author: User

One, JSP page display garbled


Second, the form is submitted in Chinese garbled


Third, database connection


Everyone in the development process of JSP, often appear in Chinese garbled problem, perhaps one to haunt you, I now put me in the development of JSP encountered


The problem of Chinese garbled and the solution to write for everyone's reference.


One, JSP page display garbled
The following display page (display.jsp) appears garbled:
&LT;TITLE&GT;JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<body>
<%
Out.print ("Chinese Processing of JSP");
%>
</body>
For different Web servers and different JDK versions, the processing results are not the same. Cause: The server uses different encoding methods and the browser


Causes different characters to display the results differently. Workaround: Specify the Encoding Method (GB2312) in the JSP page, that is, on the first page of the


Line Plus: <%@ page contenttype= "text/html; charset=gb2312 "%&gt, you can eliminate garbled. The full page is as follows



<%@ page contenttype= "text/html; charset=gb2312 "%>
&LT;TITLE&GT;JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<body>
<%
Out.print ("Chinese Processing of JSP");
%>
</body>


Second, the form is submitted in Chinese garbled
Here is a submission page (submit.jsp) with the following code:
&LT;TITLE&GT;JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<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>
Here is the processing page (process.jsp) code:
<%@ page contenttype= "text/html; charset=gb2312 "%>
&LT;TITLE&GT;JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<body>
<%=request.getparameter ("name")%>
</body>
If the submit.jsp submit English characters can be displayed correctly, if you submit the Chinese language will appear garbled. Cause: The browser uses UTF by default


-8 encoding to send the request, while UTF-8 and GB2312 encode the characters differently, so that unrecognized characters appear.


Workaround: By request.setcharacterencoding ("gb2312") The request is uniformly encoded, it realizes the normal Chinese


Show. The modified process.jsp code is as follows:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Request.secharacterencoding ("gb2312");
%>
&LT;TITLE&GT;JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<body>
<%=request.getparameter ("name")%>
</body>


Third, the database connection garbled
As long as the Chinese-related places are all garbled, the solution: in the database URL of the database plus
The USEUNICODE=TRUE&AMP;CHARACTERENCODING=GBK is OK.


Four, the database display garbled
In mysql4.1.0, the varchar type, the text type will appear in Chinese garbled, for the varchar type to set it as a binary property


Can solve the Chinese problem, for the text type is to use an encoding conversion class to handle, the implementation is as follows:
public class Convert {
/** convert 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 ("Code conversion Error:" +e.getmessage ());
Return "";
}
}
}
By compiling it into class, you can call the static method Isotogb () of the Convert class to convert the encoding.

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.