天天看點

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also

The @CODEPAGE processing directive specifies how literal (static) strings are encoded in a Web page. A code page is a character set, which can include numbers, punctuation marks, and other glyphs. Codepages are not the same for each language. Some languages, such as Japanese and Hindi, have multibyte characters, while others, such as English and German, only need one byte to represent each character. The @CODEPAGE processing directive is write-only.

A code page can be represented in a table as a mapping of characters to single-byte values or multibyte values. Many code pages share the ASCII character set for characters in the range 0x00-0x7F.

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
Syntax

<%@ CODEPAGE = codepage%>

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
Parameters

codepage
An unsigned integer that represents a valid code page for the system that is running the ASP scripting engine.

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
Remarks

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.

If @CODEPAGE is not explicitly set in a page, it is implicitly set by the AspCodePage metabase property or by the system ANSI code page.

There can be only one code page per response body, otherwise incorrect characters are displayed. If you set the code page explicitly in two pages, where one is called by the other by using #include, Server.Execute, or Server.Transfer, usually the parent page decides the code page. The only exception is if Response.CodePage is explicitly set in the parent page of a Server.Execute call. In this case, an @CodePage command in the child page overrides the parent code page.

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.

If the code page of your Web page matches the system defaults of the Web client, you do not need to set a code page in your Web page. However, setting the value is recommended.

If the code page is set in a page, then Response.Charset should also be set. The code page value specifies to IIS how to encode the data when building the response, and the charset value tells the browser how to decode the data when displaying the response. The CharsetName of Response.Charset must match the code page value, or mixed characters are displayed in the browser. Lists of CharsetNames and matching code page values can be found on MSDN Web Workshop under the columns for Preferred Charset Label and FamilyCodePage.

The file format of a Web page must be the same as the @CODEPAGE used in the page. Notepad enables you to save files in UTF-8 format or in the system ANSI format. For example, if @CODEPAGE is set to 65001 for UTF-8, the Web file must be saved in UTF-8 format. If @CODEPAGE is set to 1252, the Web file must be saved in ANSI format on an English or German system. If you want to save a page in the ANSI format for a language other than your system language, you can change your default System Locale settings in Regional and Language Options on the Control Panel. For example, after you change your system locale to Japanese, any files you save in ANSI format are saved using the Japanese code page and are only readable from a Japanese system locale.

If you are writing and testing Web pages that use different code pages and character sets (for example, if you are creating a multilingual Web site), remember that your test client computer must have the language packs installed for each language you want to display. You can install language packs from Regional and Language Options on the Control Panel.

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
Example Code

For more information, see the example for Response.CodePage.

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
Requirements

Client: Requires Windows XP Professional, Windows 2000 Professional, or Windows NT Workstation 4.0.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.

Product: IIS

@CODEPAGE SyntaxParametersRemarksExample CodeRequirementsSee Also
See Also

Reference

Session.CodePage

Response.CodePage

asp關于從utf8頁面到gb2312頁面出現亂碼得解決

昨天做了兩個asp頁面,一個是utf-8得頁面一個則是gb2312得頁面.

于是發生了件怪事(主要是我第一次遇見得),單獨打開這兩個頁面都不會出現問題,

但偏偏從utf8連接配接到gb2312得時候就出現了亂碼.花了兩個小時都搞不掉,

今天再次打開,baidu了下結果.

後來發現兩個解決方案

第一:在每個gb2312頁面上設定Session.CodePage=936

這樣問題就解決了...

第二:從utf8轉到gb2312得時候不要用<a>連接配接.用response.redirect跳轉,也能解決.

綜上,我了解為

給每個頁面設定CodePage這樣就把各個頁面得編碼都獨立起來,不再受相連頁面得影響

其次<a>标簽連接配接會将頁面編碼屬性也傳遞給下一個頁面,而使用redirect就不會出現這樣得情況了,這跟

用asp得環境變量取Request.ServerVariables("HTTP_REFERER")一樣,後者就取不到.

另附上

對于ASP編碼問題的深入研究與最終解決方案

哪的資料都不如官方資料權威。今天總算從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 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還是不能輕易用的!

繼續閱讀