`
ManGo.0317
  • 浏览: 20868 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • WEB
阅读更多

关于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" %>
分享到:
评论

相关推荐

    JSP页面中的pageEncoding和contentType两种属性 - 51CTO_COM - 注解

    JSP页面中的pageEncoding和contentType两种属性的区别与联系。

    JSP页面中的pageEncoding和contentType两种属性的区别.

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

    JSP页面pageEncoding和contentType属性

    有关于JSP页面中pageEncoding和contentType属性。

    PageEncoding&contentType中charset区别

    PageEncoding&contentType中charset区别,遇到了与大家分享!

    jsp中charset和pageEncoding

    在web编程时,通常会遇到乱码的情况,这就是我们页面编码设置问题,这个资源可以帮你解决。

    Servlet与JSP核心编程第2版

    12.2 contenttype和pageencoding属性 12.3 条件性地生成excel电子表格 12.4 session属性 12.5 iselignored属性 12.6 buffer和autoflush属性 12.7 info属性 12.8 errorpage和iserrorpage属性 12.9 ...

    JSP中的编译指令和动作指令的两点区别

    您可能感兴趣的文章:JSP常见的三个编译指令page、include、taglibJSP编译指令page、include详解JSP指令元素(page指令/include指令/taglib指令)复习整理JSP语法Page指令JSP页面pageEncoding和contentType属性JSP ...

    WEB17Project-jsp.rar 源代码

    pageEncoding:当前jsp文件的本身编码---内部可以包含contentType contentType:response.setContentType(text/html;charset=UTF-8) session:是否jsp在翻译时自动创建session import:导入java的包 errorPage:当...

    猜数题,JSP,全是JSP页面,没有SERVLET和BEAN

    *" pageEncoding="GB2312"%&gt; &lt;body&gt; &lt;form method="post" action="result.jsp" name="form1"&gt; &lt;div align="center"&gt; &lt;tbody&gt;&lt;tr&gt; 随即生成一个0到100之间的整数,请猜测! &lt;p&gt; ...

    解决JSP到MySQL数据库中文乱码问题

     这里需要设置一下JSP页面的统一编码格式:  打开Eclipse-window-&gt;preferences. 根据图片操作.  设置完后, 再新建 JSP页面的时候, 会出现下面的编码.  &lt;&#37;@ page language="java" contentType=...

    jspSmartUpload.jar

    1.下载后,直接放在web项目的WebRoot/WEB-INF/lib中,如果是实在jsp页面中使用,则在页面开始部分增加&lt;%@ import="com.jspsmart.upload.*" %&gt;(或者与util包的引入写在一起) 2.如果实在后台servlet中使用,则导入...

    jsp登陆界面源代码

    if(name.equals("abc")&& password.equals("123")) { %&gt; &lt;jsp:forward page="afterLogin.jsp"&gt; &lt;jsp:param name="userName" value="&lt;%=name%&gt;"/&gt; &lt;/jsp:forward&gt; &lt;% } else { %&gt; &lt;jsp:forward page="login.jsp...

    JSP实现简单的用户登录并显示出用户信息的方法

    本文实例讲述了JSP实现简单的用户登录并显示出用户信息的方法。分享给大家供大家参考。具体实现方法如下: login.jsp 代码如下:&lt;&#37;@ page language=”java” import=”java.util.*” pageEncoding=”GB18030...

    jsp cookie+session实现简易自动登录

    本文实例为大家分享了jsp cookie+session实现简易自动登录的具体代码,供大家参考,具体内容如下 关闭浏览器只会使存储在客户端浏览器内存中的session cookie失效,不会使服务器端的session对象失效。 如果设置了...

    jsp复习总结

    注意:一个页面可以有多个page指令,除了import外,其他属性都只能出现一次。 关于errorPage以及isErrorPage. 可能发生错误的页面: *" pageEncoding="utf-8" errorPage="2.jsp"%&gt; a) jsp页面异常处理: *" ...

    JSP上机实习报告.doc

    JSP上机实习报告 课程名称 JSP开发技术 任课老师 赵 丹 专 业 信息管理与信息系统 学 号 296 姓 名 胡 斌 JSP上机实习报告 第一题 题目 在JSP中利用Java代码计算出1+1的结果,然后在页面中显示计算结果,实现过程...

    html转jsp工具

    1、此工具只针对utf-8和gb2312两种编码格式的网页进行转换,其他编码格式的网页不支持转换! 2、此工具会自动在html页面的开始位置加入以下内容,并自动将html或者htm页面转为jsp后缀的文件 *" pageEncoding="utf-8...

    jsp连接MySQL实现插入insert操作功能示例

    在index.jsp页面输入数据,提交到mysql——insert.jsp页面进行插入数据库的操作。 index.jsp页面代码如下: 代码如下: &lt;&#37;@ page language=”java” pageEncoding=”utf-8″%&gt; &lt;&#37;@ page contentType=”...

    熟悉JSP开发环境

    建立一个Web应用,在index.jsp中显示“您好,这是我的第一个JSP站点。”; 发布测试。 四 主要代码: index.jsp页面代码: *" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE ...

    jsp各种的程序运行图

    jsp的运行程序 &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; ; charset=UTF-8"&gt; ...

Global site tag (gtag.js) - Google Analytics