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 );
}}