ASP 기본 제공 유형 및 주의 사항

30312 단어 asp
나는 다음과 같이 기억한다.
처음 배울 때는 항상 Request 유형에 미혹
처음 배울 때는 항상 Request.Form 유형 매혹
처음 배울 때는 항상 Request.Form의 유형 매혹
처음 배울 때는 늘 옳았다.item, .items, .count, .콘텐츠의 표현이 미혹되어 마지않다.
 
사실, 도무지 알 수 없는 원인은 역시 자신이 너무 게으르다는 것이다. 자료를 적게 찾았고, 대충 찾았으며, 딴마음을 품었으니, 자연히 놓친 적이 있다.
아주 간단한 일:
 1 <%
2 On Error Resume Next
3 Dim i,biName,arrBI,AI
4 biName = "ObjectContext," &_
5 "Server," &_
6 "Application," &_
7 "Application.Contents," &_
8 "Application.StaticObjects," &_
9 "Session," &_
10 "Session.Contents," &_
11 "Session.StaticObjects," &_
12 "Request," &_
13 "Response," &_
14 "Server.GetLastError," &_
15 "Request.ServerVariables," &_
16 "Request.ClientCertificate," &_
17 "Request.Cookies," &_
18 "Request.Form," &_
19 "Request.QueryString," &_
20 "Response," &_
21 "Response.Cookies," &_
22 "Request.Form(""ID"")," &_
23 "Request.QueryString(""ID"")," &_
24 "Request.Cookies(""ID"")," &_
25 "Request.ClientCertificate(""Subject"")," &_
26 "Request.ServerVariables(""URL"")," &_
27 "Server.CreateObject(""ADODB.Connection"")," &_
28 "Server.CreateObject(""ADODB.Recordset"")," &_
29 "Server.CreateObject(""Scripting.FileSystemObject"")," &_
30 "Server.CreateObject(""Scripting.Dictionary"")"
31
32 arrBI = Split(biName,",")
33 Response.Write("<table border=1 width='80%' align='center'> "& _
34 "<tr><th width='25%' nowrap='nowrap'>TypeName </th>" &_
35 "<th width='12%' nowrap='nowrap'>TypeName </th>" &_
36 "<th width='40%' nowrap='nowrap'> " &_
37 "Items,Item,Contents,Count </th></tr>" & vbNewLine)
38
39 For i = 0 To Ubound(arrBI)
40 Set AI = Eval(arrBI(i))
41 Response.Write("<tr><td align=right>" &_
42 "<span style=""color:red"">"&arrBI(i)&"<"&"/"& _
43 "span>&nbsp;</td><td align=left>" &_
44 "&nbsp;"&TypeName(AI)&"</td>")
45
46
47 If Err > 0 Then Err.Clear
48 Dim v,hasItem,hasItems
49 hasItem = False
50 hasItems = False
51 hasContents = False
52 hasCount = False
53
54 v = TypeName(AI("0"))
55 If Err > 0 Then
56 Err.Clear
57 Else
58 hasItem = "<font color=red><b>True</b></font>"
59 End If
60
61 v = TypeName(AI.Items)
62 If Err > 0 Then
63 Err.Clear
64 Else
65 hasItems = "<font color=orange><b>True</b></font>"
66 End If
67
68 v = TypeName(AI.Contents)
69 If Err > 0 Then
70 Err.Clear
71 Else
72 hasContents = "<font color=green><b>True</b></font>"
73 End If
74
75
76 v = TypeName(AI.Count)
77 If Err > 0 Then
78 Err.Clear
79 Else
80 hasCount = "<font color=blue><b>True</b></font>"
81 End If
82
83 Response.Write("<td>item:"& hasItem&" | " &_
84 "items:"& hasItems&" | " &_
85 "contents:"& hasContents&" | " &_
86 "count:"& hasCount&"</td></tr>" & vbNewLine)
87
88 Next
89 Response.Write("</table>" & vbNewLine)
90 %>

결과 출력:
TypeName 작업
TypeName 결과
Items, Item, Contents, Count 속성이 있는지 여부
ObjectContext 
 IASPObjectContext
item:False | items:False | contents:False | count:False
Server 
 IServer
item:False | items:False | contents:False | count:False
Application 
 IApplicationObject
item:True | items:False | contents:True | count:False
Application.Contents 
 IVariantDictionary
item:True | items:False | contents:False | count:True
Application.StaticObjects 
 IVariantDictionary
item:True | items:False | contents:False | count:True
Session 
 ISessionObject
item:True | items:False | contents:True | count:False
Session.Contents 
 IVariantDictionary
item:True | items:False | contents:False | count:True
Session.StaticObjects 
 IVariantDictionary
item:True | items:False | contents:False | count:True
Request 
 IRequest
item:True | items:False | contents:False | count:False
Response 
 IResponse
item:False | items:False | contents:False | count:False
Server.GetLastError 
 IASPError
item:False | items:False | contents:False | count:False
Request.ServerVariables 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.ClientCertificate 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.Cookies 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.Form 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.QueryString 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Response 
 IResponse
item:False | items:False | contents:False | count:False
Response.Cookies 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.Form("ID") 
 IStringList
item:True | items:False | contents:False | count:True
Request.QueryString("ID") 
 IStringList
item:True | items:False | contents:False | count:True
Request.Cookies("ID") 
 IReadCookie
item:True | items:False | contents:False | count:True
Request.ClientCertificate("Subject") 
 IRequestDictionary
item:True | items:False | contents:False | count:True
Request.ServerVariables("URL") 
 IStringList
item:True | items:False | contents:False | count:True
Server.CreateObject("ADODB.Connection") 
 Connection
item:False | items:False | contents:False | count:False
Server.CreateObject("ADODB.Recordset") 
 Recordset
item:False | items:False | contents:False | count:False
Server.CreateObject("Scripting.FileSystemObject") 
 FileSystemObject
item:False | items:False | contents:False | count:False
Server.CreateObject("Scripting.Dictionary") 
 Dictionary
item:True | items:True | contents:False | count:True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
참고:
1. Request의 경우Form,Request.IRequestDictionary의 유형인 QueryString은 집합 대상이며 Item 속성과 Count 방법을 가지고 있습니다.
Item의 경우 다음 참조 섹션이 있습니다.
Item 속성
집합에서 지정한 항목을 보여 줍니다.
Object.Item(natIndex) 

매개 변수
Object
EnumNetworkDrive 또는 EnumPrinterConnections 방법의 결과나 Environment 또는 SpecialFolders 속성에서 되돌아오는 대상입니다.
natIndex
읽어들일 항목입니다.
설명
Item은 각 컬렉션의 기본 속성입니다.EnumNetworkDrive와EnumPrinterConnections 집합에 있어 인덱스는 정수이다.Environment 및 SpecialFolders 컬렉션에서 색인은 문자열입니다.
요청한 폴더(strFolderName)를 사용할 수 없으면 WshShell.SpecialFolders.Item(strFolderName)은 VBScript에서 "Empty"를, JScript에서 "undefined"를 반환합니다.
 
여기서 Request에 대해출력, +-*/등의 작업에 사용될 때(TypeName,VarType 등 특수한 유형의 작업을 제외하고) Form.Form.Item() 및 Request.QueryString.Item().그것들은 수신한 매개 변수 문자열을 상응하여 되돌려준다
질의 매개변수 중 몇 개의 매개변수를 가져오려면 다음 코드를 사용합니다.
<%
Dim itemKey,itemVal
For Each itemKey In Request.QueryString
itemVal = Request.QueryString.Item(itemKey) ' :Request.QueryString(itemKey)
Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

 
2. Request의 경우"id"(및 Request.QueryString ("id")), 유형은 IStringList이며, 그것들도 집합 대상이며, Item 속성과 Count 방법을 가지고 있습니다.
질의 매개 변수에서 ID 매개 변수 항목을 가져오는 경우 다음 코드를 사용하면 심각한 오류가 발생합니다.
<%
Dim itemKey,itemVal
For Each itemKey In Request.QueryString.Item("id")
itemVal = Request.QueryString.Item("id").Item(itemKey)
' :Request.QueryString("id")(itemKey)
Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

 
그것은 각 항목을 정확하게 출력할 수 없다.포에치 때문에...IRequestDictionary와 IStringList의 요소에 대한 Next 문의 해석은 다음과 같습니다.
<%
Dim itemKey,itemVal,iItem
For iItem = 1 To Request.QueryString.Item("id").Count
itemKey = iItem
itemVal = Request.QueryString.Item("id").Item(itemKey)
' :Request.QueryString("id")(itemKey)
Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

 
표시됨, Request.QueryString 및 Request.QueryString ("id") 은 키-value 방면에서 차이가 있습니다.
전자: 각 수신된 매개 변수 이름을 각각 item의 키 이름으로 한다.for each로...next는 정확하게 매거할 수 있다
후자: 1에서count까지 각각 item의 키 이름으로 한다.for each로...next는 올바르게 열거할 수 없습니다. for...to..next 및count 결합 해결
 
이로써 item,count 및 반환 형식의 귀결은 잠시 중지됩니다...
그리고 item()에 대해 간략한 설명과 설명을 해야 한다.
다음 코드는 똑같이 실행됩니다.
<%
Response.Write "<br/>" & Request.QueryString
Response.Write "<br/>" & Request.QueryString()
Response.Write "<br/>" & Request.QueryString.Item
Response.Write "<br/>" & Request.QueryString.Item()
%>

 
다음 코드는 똑같이 실행됩니다.
<%
Response.Write "<br/>" & Request.QueryString("id")
Response.Write "<br/>" & Request.QueryString("id")()
Response.Write "<br/>" & Request.QueryString("id").Item
Response.Write "<br/>" & Request.QueryString("id").Item()
Response.Write "<br/>" & Request.QueryString.Item("id")
Response.Write "<br/>" & Request.QueryString.Item("id").Item
Response.Write "<br/>" & Request.QueryString.Item("id").Item()
%>

 
 
다음 코드는 똑같이 실행됩니다.
<%
Response.Write "<br/>" & Request.QueryString.Count
Response.Write "<br/>" & Request.QueryString().Count
%>

 
 
다음 코드는 똑같이 실행됩니다.
<%
Response.Write "<br/>" & Request.QueryString("id").Count
Response.Write "<br/>" & Request.QueryString.Item("id").Count
%>

 
주의해야 할 점: 다음과 같은 잘못된 코드 쓰기를 하지 마십시오.
 
<%
Response.Write "<br/>" & Request.QueryString.Item.Count
Response.Write "<br/>" & Request.QueryString.Item().Count

Response.Write "<br/>" & Request.QueryString("id").Item.Count
Response.Write "<br/>" & Request.QueryString.Item("id").Item().Count
%>

즉 item 및 item() 이후에는 받을 수 없습니다.Count, item ("id") 이나 item을 사용하지 않을 때만 받을 수 있습니다.Count
IRequestDictionary든 IStringList든 Item () 의 결과는 문자열이기 때문에count 호출을 할 수 없습니다
Request.QueryString.Item()의 결과는 모든 질의 매개변수에 문자열로 표시됩니다.
Request.QueryString.Item("id").Item() 또는 Reuqest.QueryString("id").Item()의 결과는 모든 질의 매개변수 ID의 매개변수 이름 값으로 문자열로 나열됩니다.
 
기타: Dictionary 사전 대상의 집합만이 Items () 방법이 있습니다. item () 또는 item (키) 와 헷갈리지 마십시오.
사전의items () 방법은 for와 결합하기 위해 변수 그룹을 되돌려줍니다.to .. next 및 Ubound() 작업
사전에는 item 및count 속성도 있습니다
For each 호환 사전...next 조작
 
Application 및 Session에는 Item과 유사한 Contents 속성이 있습니다. 집합 대상인 IVariantDictionary를 되돌려줍니다.IRequestDictionary와 동일하게 작동
Application 및 Session에는 Item 속성도 있습니다.??(물론 아니야, 함수를 검증하는 문제야!!)
 
본문 참조:
http://msdn.microsoft.com/zh-cn/library/windows/apps/aa650188
『Windows 스크립트 기술』

좋은 웹페이지 즐겨찾기