The relationship between pageEncoding and charset in contentType and detailed explanation of garbled characters

1. Operating environment: Tomcat8.0

Second, the difference between charset and pageEncoding in the jsp file:

1. The charset of contentType refers to the content encoding when the server sends it to the client
将charset为UTF-8,那么在浏览器当前网页右键-->编码,可以看到浏览器选择的编码也是UTF-8,如果charset设置为GBK,浏览器编码则会选择为GBK。
2.pageEncoding refers to the output mode of jsp page
设置pageEncoding设置为GBK,无论这个jsp文件实际是什么编码方式,都会以GBK的格式输出;pageEncoding还有一个功能就是告诉IDE这个文件是
什么编码格式,以便于IDE自动修改文件编码;这里用Eclipse测试(其他IDE未尝试):
windows环境下选中jsp文件 右键-->属性 可以看到下图:

Write picture description here

如果Default选项为选中状态,当改变pageEncoding时,Eclipse会自动改变jsp文件的编码格式。

3. Reasons and solutions for jsp page garbled characters

1. Improper setting of pageEncoding causes garbled characters
关于pageEncoding的取值分为下面三种情况(charset的取值原则与此一致):
  1. Specify the value of pageEncoding, pageEncoding is the specified value;
  2. Only the value of charset, pageEncoding thinks it is the same as the value of charset;
  3. If there is neither pageEncoding nor charset, the system will take the default value "ISO-8859-1"

当jsp页面pageEncoding指定的编码和jsp页面的实际编码不一致时,如上图文件编码选择了Other的GB2312,如果此时pageEncoding指定的编码
不是GB2312就会出现乱码。
2. The encoding specified by charset does not contain all the text in the jsp page
当charset的编码中不含有当前jsp页面中的某些字符时,会出现乱码情况;如:jsp页面中含有中文字符,charset为“ISO-8859-1”(可以保存,
发布),pageEncoding为GBK(其他任何Tomcat支持的含有中文字符的编码都行,UTF-8 , gb2312等;ANSI为windows下的编码格式,tomcat
不支持,无法解析)时,访问该页面会出现乱码,这是由于“ISO-8859-1”编码不含有中文字符,无法正确解析导致。

另外,如果pageEncoding指定的编码不含有jsp页面中的所有字符,Eclipse是不让保存的,如:pageEncoding=ISO-8859-1,jsp页面含有中文,
当保存时Eclipse会弹出下面的对话框:

Write picture description here

3. Processing of garbled characters
综上所述:为了保证jsp不会乱码,在Eclipse下我们可以这么做:
  1. Select the default option Default for the jsp encoding format, see Figure 1;
  2. Do not set the encoding of charset;
  3. pageEncoding must be set (in fact, within the scope of tomcat's recognition, it doesn't matter what it is, as long as it meets the company's specifications);

postscript

contentType="text/html; charset=UTF-8"对应于该jsp界面相应java文件的这里,如下图:

Write picture description here

Guess you like

Origin blog.csdn.net/niuzhucedenglu/article/details/59719105