asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

哪的资料都不如官方资料权威。今天总算从MSDN中择出了ASP编码问题的解决方案。

下面是MSDN中的一段话。

Setting @CODEPAGE explicitly affects literal strings in a single response.Response.CodePage affects dynamic strings in a single response, and Session.CodePage affects dynamic strings in all responses in a session.

这句话解释清楚了@CODEPAGEResponse.CodePage,Session.CodePage 分别的作用是什么。

@CODEPAGE作用于所有静态的字符串,比如某文件中的 const blogname="我的家"

Response.CodePage,Session.CodePage作用于所有动态输出的字符串,比如<%=blogname%>

这句话很关键的是说明了Response.CodePage的作用范围是a single response,而SXNA中声明的Session.CodePage的作用范围是all responses in a session。

再看另外一句话。

If Response.CodePage is not explicitly set in a page, it is implicitly set by Session.CodePage, if sessions are enabled. If sessions are not enabled, Response.CodePage is set by @CodePage, if @CodePage is present in the page. If there is no @CodePage in the page, Response.CodePage is set by the AspCodePage metabase property. If the AspCodePage metabase property is not set, or set to 0, Response.CodePage is set by the system ANSI code page.

这句话我乍一看,把意思理解成了这样:在sessions are enabled的时候,如果Response.CodePage没有声明,则Response.CodePage会被Session.CodePage赋值。如果sessions are not enabled的时候, 如果@CodePage已声明,则Response.CodePage会被@CodePage赋值,等等.............

这句话解释了为什么从SXNA中出来以后进入一些别的页面比如oblog,z-blog等等容易出现乱码,因为其他程序没有声明Response.CodePage而恰巧SXNA声明了Session.CodePage,因此一进入SXNA,Session.CodePage立即被赋值(版本不同,有的版本赋了936有的版本赋了65001),而后进入其他程序的时候Response.CodePage马上被Session.CodePage赋值如果这时Response.CodePage与页面本身编码不一样的话,页面就会出现乱码。所以进入z-blog出现乱码的时候我查了当时的Session.CodePage和Response.CodePage都是936,而进入oblog出现乱码的时候Session.CodePage和Response.CodePage都是65001.就是说要想保证叶面不出现乱码,应该声明Response.CodePage,否则他就会按照Session.CodePage来解释网页(而不是按照@codepage解释网页).

如果仅仅按照上面的解释的话,我实际上是很糊涂的,因为我们都是用的中文操系统,当每一次进入浏览器的时候你可以尝试输出Session.CodePage,能看到他都是936!为什么进入Z-blog的时候他不把默认的Session.CodePage的936赋给Response.CodePage呢?反而把@CodePage给了Response.CodePage?什么情况下Session.CodePage才赋值给Response.CodePage呢?原文的sessions are enabled应该如何理解呢?

也许上面的话应该这样理解:

在Session.CodePage任何程序声明的时候,如果Response.CodePage没有声明,则Response.CodePage会被Session.CodePage赋值。如果Session.CodePage没有被任何程序声明的时候, 如果@CodePage已声明,则Response.CodePage会被@CodePage赋值,....,最后的页面动态内容部分按照Response.CodePage的值解释。

因为Zblog和Oblog都声明了@CodePage,所以,用户刚刚启动完机器然后进入浏览器浏览Zblog和Oblog的时候Response.CodePage会被@CodePage赋值,于是叶面显示正常。

这句话进一步解释了产生乱码的原因

If you set Response.CodePage or Session.CodePage explicitly, do so before sending non-literal strings to the client. If you use literal and non-literal strings in the same page, make sure the code page of @CODEPAGE matches the code page of Response.CodePage, or the literal strings are encoded differently from the non-literal strings and display incorrectly.

其中比较有用的一句话是说如果Response.CodePage@CODEPAGE不一样的话会产生乱码。也就是说当Z-blog的@CODEPAGE=65001而Z-blog的Response.CodePage被Session.CodePage赋为936的时候就会出现乱码,oblog反之亦然。

不知道上面说了这么多解释清楚没有-_-||

下面解释一下为什么SXNA有时会把Session.CodePage赋为936,我有一个版本是这样写的:

<% OriginalCodePage=Session.CodePage %>

.......

<% Session.CodePage=OriginalCodePage %>

当用户进入浏览器的时候Session.CodePage默认为936,这个时候的默认936不是程序声明的,因此不会赋给Response.CodePage,当进入SXNA的时候,Session.CodePage被上面那段代码一折腾就变成了程序声明的Session.CodePage=936,因此再进入Zblog的时候就把936给了Response.CodePage

至此,全部原因已经分析清楚了。

因此说,保证asp叶面一定不会出现乱码的代码应该是这样的:(假定是UTF-8的叶子)

<%@ CODEPAGE=65001 %>

<% Response.CodePage=65001%>

<% Response.Charset="UTF-8" %>

进一步说明为什么要加Response.Charset,因为MSDN说应该加...呵呵

If the code page is set in a page, then Response.Charset should also be set.

另外,文件的编码格式应该与@CODEPAGE一样:

The file format of a Web page must be the same as the @CODEPAGE used in the page.

这就是为什么zblog,pjblog等一些程序要吧文件存成UTF8编码格式的原因.

综上,如果所有的程序都声明了Response.CodePage就不会被Session.CodePage干扰而出现乱码了。所以Session.CodePage还是不能轻易用的!

posted on 2007-01-17 08:28 汪杰 阅读(462) 评论(2)  编辑 收藏 引用 所属分类: asp(javascrpt)

FeedBack:
# re: 哪的资料都不如官方资料权威。今天总算从MSDN中择出了ASP编码问题的解决方案。
2007-11-12 13:43 | 汪杰
<%@Language=Javascript CodePage=65001%>
<%
Response.CodePage=65001;
Response.Charset="UTF-8";
%>  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 456690
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜