【转载+整理】关于JSP页面中的pageEncoding和contentType两种属性的区别

关于JSP页面中的pageEncoding和contentType两种属性的区别:

  pageEncoding是jsp文件本身的编码

  contentType的charset是指服务器发送给客户端时的内容编码

 

  JSP要经过两次的“编码”,第一阶段会用pageEncoding,第二阶段会用utf-8至utf-8,第三阶段就是由Tomcat出来的网页, 用的是contentType。

  第一阶段是jsp编译成.java,它会根据pageEncoding的设定读取jsp,结果是由指定的编码方案翻译成统一的UTF-8 JAVA源码(即.java),如果pageEncoding设定错了,或没有设定,出来的就是中文乱码。

  第二阶段是由JAVAC的JAVA源码至java byteCode的编译,不论JSP编写时候用的是什么编码方案,经过这个阶段的结果全部是UTF-8的encoding的java源码。

  JAVAC用UTF-8的encoding读取java源码,编译成UTF-8 encoding的二进制码(即.class),这是JVM对常数字串在二进制码(java encoding)内表达的规范。

  第三阶段是Tomcat(或其的application container)载入和执行阶段二的来的JAVA二进制码,输出的结果,也就是在客户端见到的,这时隐藏在阶段一和阶段二的参数contentType就发挥了功效

 

  contentType的設定.

  pageEncoding 和contentType的预设都是 ISO8859-1. 而随便设定了其中一个, 另一个就跟着一样了(TOMCAT4.1.27是如此). 但这不是绝对的, 这要看各自JSPC的处理方式. 而pageEncoding不等于contentType, 更有利亚洲区的文字 CJKV系JSP网页的开发和展示, (例pageEncoding=GB2312 不等于 contentType=utf-8)。

  jsp文件不像.java,.java在被编译器读入的时候默认采用的是操作系统所设定的locale所对应的编码,比如中国大陆就是GBK,台湾就是BIG5或者MS950。而一般我们不管是在记事本还是在ue中写代码,如果没有经过特别转码的话,写出来的都是本地编码格式的内容。所以编译器采用的方法刚好可以让虚拟机得到正确的资料。

  但是jsp文件不是这样,它没有这个默认转码过程,但是指定了pageEncoding就可以实现正确转码了。

  举个例子:

 

 

<%@ page contentType="text/html; charset=UTF-8" %>

 

  大都会打印出乱码,因为输入的“你好”是gbk的,但是服务器是否正确抓到“你好”不得而知。

  但是如果更改为

 

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="GBK" %>

  这样就服务器一定会是正确抓到“你好”了。

 

 

-----------------------------------------------------修整的 分割线----------------------------------------------------------

 

 

SUN官方解释(见《The Java EE 5 Tutorial 》)

Response and Page Encoding

You also use the contentType attribute to specify the encoding of the response. For example, the date application specifies that the page should be encoded using UTF-8, an encoding that supports almost all locales, using the following page directive:

 

Java代码 复制代码
  1. <%@ page contentType="text/html; charset=UTF-8" %>  
<%@ page contentType="text/html; charset=UTF-8" %>

 

If the response encoding weren’t set, the localized dates would not be rendered correctly.

 

To set the source encoding of the page itself, you would use the following page directive:

 

Java代码 复制代码
  1. <%@ page pageEncoding="UTF-8" %>  
<%@ page pageEncoding="UTF-8" %>

 

You can also set the page encoding of a set of JSP pages. The value of the page encoding varies depending on the configuration specified in the JSP configuration section of the web application deployment descriptor (see Declaring Page Encodings).

 

Declaring Page Encodings

You set the page encoding of a group of JSP pages using the JSP property group configuration in the deployment descriptor by doing one of the following:

  • If you are using the Pages section of the web.xml editor pane in NetBeans IDE:

    1. Expand the JSP Property Group node.

    2. Enter the page encoding in the Page Encoding field.

  • If you are editing the web.xml file by hand, add a page-encoding element to the jsp-property-group element in the deployment descriptor and set it to one of the valid character encoding codes, which are the same as those accepted by the pageEncoding attribute of the page directive.

A translation-time error results if you define the page encoding of a JSP page with one value in the JSP configuration element and then give it a different value in a pageEncoding directive.

 

一般HTML文档有

 

Html代码 复制代码
  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

来标识客户端编码,即指定访问服务器的浏览器是用什么编码.

<%@ page contentType="text/html; charset=UTF-8" %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值