您的位置:首页 > 编程语言 > ASP

对于ASP编码问题的深入研究与最终解决方案

2007-09-20 11:47 615 查看

对于ASP编码问题的深入研究与最终解决方案

.style4 {color: #9900FF;font-weight: bold;}.style9 {color: #9900FF}.style10 {color: #FF00FF}

哪的资料都不如官方资料权威。今天总算从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还是不能轻易用的!

参考文章:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/268f1db1-9a36-4591-956b-d7269aeadcb0.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/582e6f47-52eb-413e-8b5d-c99145cb61d8.asp

Tags: CodePage ASP 编码 乱码
、、相关文章:

UTF-8 Unicode Ansi 汉字GB2321几种编码转换程序 (2005-7-10 23:17:48)

Ajax中的responseText编码问题(引用)

冰狗的部落

在使用Ajax过程中,有个头疼的问题,responseText传回的值不可更改的为utf-8,这导致页面编码很头疼,统一用utf-8是没有问题,但为了压缩页面大小,还是有很多开发者采用 charset=ISO-8859-1(英文)和charset=gb2312(简体中文),那么该如何处理呢?我们目前的开发是用Asp,经实践可以用js的escape()和unescape()方法来解决上面的问题。具体来说,在服务器端将response.write的内容用escape()方法预处理

2006-2-7 14:42:53

对于ASP编码问题的深入研...(引用)

寻找白云

2006-2-26 21:47:18

对于ASP编码问题的深入研究与最终解决方案(引用)

888 占星家俱樂部

.style4 {color: #9900FF;font-weight: bold;}.style9 {color: #9900FF}.style10 {color: #FF00FF}哪的资料都不如官方资料权威。今天总算从MSDN中择出了ASP编码问题的解决方案。<table width="95%" border="0" cellpadding="12" cells

2006-3-27 21:43:38

引用sipo的对于ASP编码问题(引用)

Aspid Blog

最近在做Unicode编码,真是头痛的问题,搞了N久头晕了.终于在群里讨论了下,找到Sipo一篇比较综合的解决方法.下面是sipo的对于ASP编码问题的深入研究与最终解决方案:

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

下面是MSDN中的一段话。
...

2006-4-6 16:52:41

再次被乱码搞得头痛,查资料吧!(引用)

行者途中

哪的资料都不如官方资料权威。今天总算从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 s

2006-5-16 15:12:35



1.ZBLOG爱好者

document.write(unescape('http%3A//www.kenwong.cn')); http://www.kenwong.cn
在偶眼里,代码都是一样的:不懂。

纯支持一下,辛苦了!


2005-11-26 18:18:49 回复该留言



2.phevoz

document.write(unescape('http%3A//www.uleea.com/blog')); http://www.uleea.com/blog
If the AspCodePage metabase property is not set, or set to 0, Response.CodePage is set by the system ANSI code page.
明白
我以前在QQ上问过SIPO之前的SXNA加这段代码的原因
<%@ CODEPAGE=65001 %>
<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>
<% Response.Buffer=True %>

2005-11-26 20:01:21 回复该留言



3.sipo

document.write(unescape(''));

不明白2楼的兄弟想表达什么意思?
SXNA没有用过<% Response.CodePage=65001%>
这段代码。其实应该用的。
system ANSI code page是windows应用程序默认的一个属性(比如记事本),另外AspCodePage metabase property 是IIS的一个属性,都是可以被@CODEPAGE覆盖掉的,没有用。因为大多数程序都加入了@CODEPAGE,所以就不讨论那两个了。

2005-11-26 20:38:20 回复该留言



4.zx.asd

document.write(unescape('http%3A//www.rainbowsoft.org/')); http://www.rainbowsoft.org/
3Q,把Z-BLog全加上<% Response.CodePage=65001%>就不会出现乱码了.



2005-11-27 7:56:00 回复该留言



5.Chobits

document.write(unescape(''));

Thank you!我正为我的asp网页编码问题头疼呢!
楼主讲的真清楚,我现在终于明白了!

2005-12-28 12:15:23 回复该留言



6.trip

document.write(unescape('http%3A//www.chinese-tours.com/')); http://www.chinese-tours.com/
请版主解答上面的问题,谢谢.在线等

2006-2-14 0:36:13 回复该留言



7.sipo

document.write(unescape('http%3A//www.dc9.cn')); http://www.dc9.cn
1,z-blog改日文不需要动任何其他文件文件,在language下面增加日文语言包并存成UTF-8文件格式就可以了
关于如何存成UTF-8文件格式请用editplus

2006-2-14 9:00:54 回复该留言



8.trip

document.write(unescape('http%3A//www.chinese-tours.com/')); http://www.chinese-tours.com/
版主我按你说的办法增加日文语言包和存为utf-8的文件,用editplus编辑的。

2006-2-15 21:51:31 回复该留言



9.sipo

document.write(unescape('http%3A//www.dc9.cn')); http://www.dc9.cn
你在c_custom.asp里面加入<%Session.CodePage=932%>

2006-2-15 22:23:33 回复该留言



10.sipo

document.write(unescape('http%3A//www.dc9.cn')); http://www.dc9.cn
顺便说一下,我说的方法在zblog原版里应该没问题。plus里就不知道了。

2006-2-15 22:29:19 回复该留言



11.whcn

document.write(unescape(''));

good!thx!!!!!!!!!!

2007-1-1 4:55:29 回复该留言



12.请问

document.write(unescape(''));

请问oblog该如何修改 ,我的网站日文出现乱码 变成了数字
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: