ASP中UTF-8乱码的问题2009-11-16 22:291,<%@codepage="65001"%>
2,
3,Session.CodePage = 65001
4,文件存成 UTF-8
以上都试了,从数据库读取的信息还是会经常乱码(有时也会正常一下);
为何判断是数据库读取的问题,因为所有从数据库读取的信息直接就是乱码;
Access数据库已经转为繁体了;
还在找寻中。。。
========================================================
网上有误传,说没法解决????
还是先说解决方法,每个对外显示的画面都必须加上,特别是针对UTF-8编码:
<%@LANGUAGE="
VBSCRIPT" CODEPAGE="65001"%>
<% response.charset="utf-8" %>
<%Session.CodePage=65001%>
文件保存为 UTF-8编码格式;
========================================================
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.
这句话解释清楚了@CODEPAGE,Response.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 e