Analysis and Solution of JSP page garbled characters
I. Fix garbled characters in the response
The garbled code in the response is to display the garbled code on the page, because the page data is put into the response from the server end, and then sent to the browser. If the data in the response cannot be properly parsed, garbled characters may occur.
Why is there no problem in English? Because in the iso-8859-1, gb2312, UTF-8 and any encoding format, the English encoding format is the same, each character occupies 8 digits, and the Chinese language is troublesome, in gb2312, the next Chinese Character occupies 16 bits and two bits, while in UTF-8, the next Chinese Character occupies 24 bits and three bits. When the browser does not know the encoding method, the characters will be truncated from the middle, and the display will be out of order. To solve the garbled code problem, we need to tell the browser what encoding method we use.
Note the following steps to get the properly displayed Chinese characters:
Because the server needs to read the JSP file locally and then write the response after processing, we must first know the encoding format of the JSP file. Solve the problem from the source.
In Windows XP, the default file encoding format is gb2312. We need to add the encoding information in the HTTP Response (response), using the following method:
<% @ Page contenttype = "text/html; charset = gb2312" %>
This section is placed in the first line of the JSP page to specify the response type and encoding format. contenttype is text/html, Which is HTML content, and charset indicates encoding is gb2312. In this way, the browser can obtain the encoding format from the response. This form of <% @ %> is called the JSP instruction (Directive). Now we are exposed to the page instruction, including the include and taglib commands, which will be discussed later.
You also need to specify the encoding format in HTML.
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> title </title>
</Head>
The meta part is used to specify the current HTML encoding format. Note that this section should be placed in the head tag and placed at the beginning of the head tag. If it is not at the beginning, problems may occur in IE, especially when the title contains Chinese characters.
II. Post garbled
First, add method = "Post" to form to enable post when submitting the form.
When sending a request, the encoding used is iso-8859-1, meaning that only English is a valid character, this restriction is because at the beginning of the specified HTTP standard members are from the English country, therefore, if you use the default method to retrieve data from the request, all Chinese characters will be garbled.
The solution is to add a Java statement at the beginning of JSP to set the character encoding of the request.
<%
Request. setcharacterencoding ("gb2312 ");
%>
3. Get garbled
Click the hyperlink directly. The default submission method of form is get.
The solution in post mode is simple, because the data submitted in post mode is sent in binary mode and appended to the body part of the HTTP request, you only need to specify the encoding format in the background.
In get mode, parameters are directly appended to the URL. The request. setcharacterencoding () cannot be used for processing these parameters. The result is that all the Chinese files in get form are garbled. There is no easy way to convert these Chinese characters one by one and use new string (bytes, "gb2312") for transcoding.
As we can see, first get the parameters from the request, then the string according to the iso-8859-1 encoding into a byte array, and then use gb2312 encoding into a new string, and finally print out is normal Chinese. The disadvantage of this is that transcoding is required for all Chinese files obtained from the request, which is very cumbersome. Therefore, do not <a href = "test. jsp? Username = test "> test </a>: Write the Chinese parameters to the hyperlink, and try to use method =" Post "for form. In this way, you only need to set request. setcharacterencoding () can handle Chinese garbled characters.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/CenturyMagus/archive/2008/07/23/2697479.aspx