JSP page garbled problem

Source: Internet
Author: User

1. When the form is submitted in Chinese, garbled characters appear.

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

<html>

<head>

<title> JSP's 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>

Here is the processing page (process.jsp) code:

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

<html>

<head>

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

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

</head>

<body>

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

</body>

</html>

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-8 encoding to send the request by default , while UTF-8 and GB2312 encode the characters differently, which makes the unrecognized character appear. Solution: By request.secharacterencoding ("gb2312") the request is uniformly encoded, the normal display of Chinese is realized. The modified process.jsp code is as follows:

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

<%

Request.secharacterencoding ("gb2312");

%>

<html>

<head>

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

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

</head>

<body>

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

</body>

</html>

2, database connection garbled

As long as the Chinese language is all garbled, the solution: in the database URL of the database plus USEUNICODE=TRUE&CHARACTERENCODING=GBK is OK.

3, the database display garbled

In mysql4.1.0, the type of varchar, the text type will appear in Chinese garbled, for the varchar type set it to binary attribute can solve the Chinese problem, for the text type is to use an encoding conversion class to handle, the implementation is as follows:

public string ISO2GB (string qs)

{

try{

if (QS = = null) return "NULL";

Else

{

return new String (Qs.getbytes ("iso-8859-1"), "gb2312");

}

}

catch (Exception e) {

SYSTEM.ERR.PRINTLN ("ISO2GB Error:" +e.getmessage ());

}

return "NULL";

}

public string Gb2iso (string qs)

{

Try

{

if (QS = = null) return "NULL";

else {

return new String (Qs.getbytes ("gb2312"), "iso-8859-1"); }

}

catch (Exception e) {System.err.println ("Gb2iso Error:" +e.getmessage ());}

return "NULL";

}

This article is for reference only.

JSP page garbled problem

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.