SQL Server Chinese garbled characters (for other databases, refer)

Source: Internet
Author: User


In my process, we use GBK or gb2312 encoding format. We haven't tried UTF-8.

1) JSP page:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "GBK" %>

// 1. the following sentence is one of the key points to solve Garbled text. The following sentence not only affects Chinese characters, but also the layout. I used UTF-8 in the previous layout and now changed it to gb2312. I put it in IE8 for layout disorder, firefox is normal.

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

<% Request. setcharacterencoding ("gb2312"); %>

2) on the server side, Servlet:

Convert the parameters obtained from the interface to gb2312, because the encoding of parameters passed by the browser is iso-8859-1 by default.

String searchw = Req. getparameter ("searchword ");
// 2. The following sentence is one of the keys to solve Garbled text.
String searchword = new string (searchw. getbytes ("iso-8859-1"), "gb2312 ");

I have tested the following two sentences. If the above steps are correct, you can display Chinese characters. But I added

Req. setcharacterencoding ("gb2312 ");
Resp. setcontenttype ("text/html; chartset = gb2312 ");

3) on the Database End, set the database encoding to GBK.


Today I also encountered a headache: If you change chartset UTF-8 to gb2312, the web page layout in IE8 will be a bit messy. Firefox is still normal, and garbled characters will appear if it is not changed to gb2312 Chinese data. Therefore, the page encoding affects the layout in IE8. (Other IE versions are not tested)

<% @ Page contenttype = "text/html; charset = UTF-8" %> <! -- This sentence decide the CSS and the unreadable code -->

Later, I saved all the CSS files as GBK encoding formats and deleted all Chinese garbled characters in CSS. The layout in IE8 was restored to normal.

The Java program for viewing the operating system code is included:

Public classshowsystemdefaultencoding {

Public static void main (string [] ARGs ){

String encoding = system. getproperty ("file. encoding ");

System. Out. println (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.