JSP page display garbled solution __jsp

Source: Internet
Author: User
One, JSP page display garbled

The following display page (display.jsp) will appear garbled:

<TITLE>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. Reason: The server uses different coding methods and browsers

The result is different for different characters to display the results. Workaround: Specify the Encoding Method (GB2312) in the JSP page, that is, the first line of the page plus: <%@ page contenttype= "text/html;" charset=gb2312 "%>, you can eliminate garbled." The complete page is as follows

<%@ page contenttype= "text/html; charset=gb2312 "%>

<TITLE>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 submitted in Chinese when there are garbled

Below is a submission page (submit.jsp) with the following code:

<TITLE>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>

The following is the Process page (process.jsp) code:

<%@ page contenttype= "text/html; charset=gb2312 "%>

<TITLE>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 the submission of Chinese will appear garbled. Reason: Browsers use UTF-8 encoding to send requests by default, while UTF-8 and GB2312 encodings are different when they represent characters, which can result in unrecognized characters.

Solution: Through the request.setcharacterencoding ("gb2312") to the request for unified coding, the realization of the normal Chinese display. The modified process.jsp code is as follows:

<%@ page contenttype= "text/html; charset=gb2312 "%>

<%

Request.setcharacterencoding ("gb2312");

%>

<TITLE>JSP's Chinese processing </title>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>

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

</body>

third, the database connection appears garbled

As long as the Chinese-related places are all garbled, the solution: in the database URL to add

The USEUNICODE=TRUE&CHARACTERENCODING=GBK is OK.

Four, the database display garbled

In mysql4.1.0, varchar type, text type will appear in Chinese garbled, for the varchar type to set it as binary attribute can solve the Chinese problem, for text type to use a code conversion class to deal with, realize as follows:

public class Convert {

/** converts the iso-8859-1 code into 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 class, you can invoke the static method Isotogb () of the Convert class to convert the encoding.

Summarize:

1. In the JSP <%@ page contenttype= "text/html; Charset=a "%> if specified, all constructed strings (not references) in the JSP are changed, and if no encoding is specified, the string is encoded as a. The string obtained from request is iso-8859-1 if no code is specified for request. The string that is obtained from elsewhere is encoded using the original, such as a string from the database, and if the encoding of the database is B, then the string's encoding is B and not a, nor is the system default. At this point, if the encoding of the string to be exported is not a, then it is likely to display garbled, so the first thing to do is to convert the string to a string of encoded a, and then output.

2. In the JSP <%@ page contenttype= "text/html; Charset=a "%> is not specified, then corresponds to the designation <%@ page contenttype=" text/html; Charset=iso-8859-1 "%>

3. If a response.setcontenttype ("Text/html;charset=a") is executed in the Servelte, the character output stream encoding for response is set to a, and all strings to be exported are encoded into a, Otherwise, it will get garbled.

The string encoded in Servelet from the request is the same as in the JSP, but the string constructed in the servlet Java file is the default encoding of the system used. Strings that are obtained externally from the Servelt are encoded using the original encoding, such as the data obtained from a database encoded as B, which is encoded as B, not a, or the system default 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.