First, let's look at a header for the JSP file generated by eclipse:
Figure 1:jsp File encoding
as shown in the JSP three encoding,
① represents the encoding of the server in response to client requests. The server uses it to set the HTTP response header's Content-type. It's like using Response.setcontenttype ("Text/html;charset=utf-8") in a servlet. You can see it in the Content-type in the header of the browser.
②pageenconding encoding is the encoded format in which the value JSP file itself is encoded. Because the JSP will eventually be compiled into a CLSS bytecode, and the bytecode is Unicode encoded, you must know the format to be converted. If not set, eclipse encodes it by iso-8859-1 encoding. If 1 is not set, then the 2pageEncoding encoding is used to set it.
The Content in ③ is also set to the browser's decoding format, but its priority does not have a high content-type priority in the header, so as long as the Content-typeis set in the header, Then the content of meta is not used in the basic.
Here's a very simple example to test:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Very simple, run without garbled. Use the Firefox debug to look at the request header as follows:
Figure 2:header1
If you are interested, you can try to Figure 1 ① encoding contenttype removed, found still no garbled, look at the response header information, Content-type encoding is still utf-8, This is because if you do not set the encoding of ① in Figure 1 contenttype the default is to use the ②pageencoding encoding in Figure 1.
You can also try to change the encoding in the ③ to iso-8859-1 and other incompatible with the Chinese encoding, found still not garbled, view the request header information Content-type encoding is still utf-8, And the priority of encoding in meta is higher, so the browser still uses the Utf-8 in the response header to decode.
Now we change the encoding contenttype encoding of ① to GBK, for example:
Figure 3:CONTENTTYPE-GBK
In Firefox debug check the request header information Content-type encoding, found that Content-type encoding is GBK so that ① encoding contenttype is set in the response header content_type. But Strange is not garbled, the server is clearly utf-8 code, the response head is GBK Why did not appear garbled it? No solution for the moment.
But we put in ① encoding contenttype code to iso-8859-1 incompatible with the Chinese encoding method, found that there is garbled.
There is no garbled reason in Figure 3, perhaps we can find some hints from the browser decoding process, please refer to:
Www.w3.org/html/wg/drafts/html/master/syntax.html#parsing-with-a-known-character-encoding
Java Web Coding Problem One: The coding problem of JSP