XML在ASP中的一些運用

來源:互聯網
上載者:User
xml   使用可擴充標誌語言(XML)編寫的指令碼和組件和.XML被用來定義對象,方法,屬性等等。
  同時指令碼還提供函數。指令碼語言可以是javascript, vbscript等等。運用在網站的運用程式和網站的普通例子,現在都可以被轉換成scriptlets. 只要使用scriptlets 來寫組件,他們可以再被從新使用在網站或則運用程式各部分,既可以使用在服務端(例如ASP代碼)也能夠被使用在用戶端(例如瀏覽器)。

這裡將集中討論服務端的用來組件ASP代碼的Scriptlets。
早些時候,ASP開發人員習慣寫include檔案。用來做公用的服務端例行程式,
還用在他們的網站運用程式中。從功能上說,include檔案被局限在僅僅是在頁面中引用。
完成傳遞參數的功能。
現在同樣的公用例行程式能夠中添加了scriptlet方法和並且有助與在運用程式中使用組件。
這個方法現在非常普遍的運用在網站運用程式中,能夠被運行在同一個伺服器上的運用程式重用。
這個方法使得從資料庫中擷取(pull-down)資料將變得非常通俗化。

例子:建立scriptlet方法和屬性,使得很通俗的從資料庫中擷取資料。
並且使得運行在同一個伺服器上所有的運用程式使用這個同樣的scriptlet,而不用重新寫代碼了。

代碼如下:
scriptlet檔案名稱為scp_query2list.sct

 <?XML version="1.0"?>
 <scriptlet>
 <registration
     description="scp_query2list"
     progid="scpquery2list.Scriptlet"
     version="1.00"
     classid="{e32d2a70-4e11-11d3-a9f0-0080c8034244}"
 >
 </registration>

 <public>
     <method name="query2list">
         <PARAMETER name="objConn"/>
         <PARAMETER name="query"/>
     </method>
 </public>

 <implements type="ASP" id="ASP"/>
 <script language="VBScript">

 <![CDATA][
     Sub query2list(objConn, query)     
         Dim objRs
         Set objRs = objConn.Execute(query)
             If objRs.EOF AND objRs.BOF Then
                 Response.write "<option value='none'>沒有找到記錄。</option>"
             Else
                 Do While Not objRs.EOF
                     Response.write "<option value='" & objRS(0) & "'>" & objRs(0) & "</option>"
                     objRs.MoveNext
                 Loop
             End If
         objRS.Close
         Set objRS = nothing
     End Sub
 ]]>

 </script>
 </scriptlet>
其中在scriptlets中使用的各種元素的具體說明可以在http://msdn.microsoft.com/scripting處找到。
下面是怎麼具體調用scriptlets的代碼:(只要是在同一個伺服器上註冊了scriptlets的運用程式,都可以使用這段代碼)
query2list.asp 檔案:
 <%@ Language=VBScript %>
 <%

     Option Explicit

     Dim objConn
     Dim dbPath
     Dim sql
     Dim objScpQuery2list

     Set objConn = Server.CreateObject ("ADODB.Connection")
     dbPath="DBQ=" & Server.MapPath("techpapers_test.mdb")

     objConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & dbPath

     '--- query required for pull down
     sql = "select AuthorFirst from Papers"
     Set objScpQuery2list = Server.CreateObject ("scpquery2list.Scriptlet")
 %>
 <html>
 <body>
 <center>
 <form name = "fr1">
     <select>
     <option value="none" selected>選擇發行人</option>
     <% call objScpQuery2list.query2list(objConn, sql) %>
     </select>
 </form>
 </center>
 </body>
 </html>

 <%
     objConn.Close
     Set objConn = nothing
     Set objScpQuery2list = nothing
 %>

出現的問題:
第一次使用scriptlets有可能會出現這樣的問題,就是記憶體浪費現象,改進方法如下:
將ASP對象傳遞給scriptlets,而代替使用ASP介面。(就是不使用 <implements type="ASP" id="ASP"/>申明)
testscp.sct:
     <?XML version="1.0"?>
 <scriptlet>
 <?scriptlet error="true" debug="true"?>
 <registration
         description="testscp"
         progid="testscp.Scriptlet"
         version="1.00"
         classid="{78a33aa0-335d-11d3-a9a9-0080c8034244}"
 >
 </registration>

 <public>
      <property name="cnnState">
       <get/>
      </property>
      <method name="TestWrite">
      <parameter name="sHTML"/>
   </method>
      <method name="TestWriteLine">
   <parameter name="sHTML"/>
   </method>
   <method name="ASPConnect">
   <parameter name="oServer"/>
       <parameter name="oApplication"/>
       <parameter name="oSession"/>
      <parameter name="oRequest"/>
   <parameter name="oResponse"/>
   </method>
 </public>
 <script language="VBScript">
 <![CDATA][
  Private cnnState, Server, Application, Session, Request, Response
  cnnState = False

  Function get_cnnState()
   get_cnnState = cnnState
  End Function
 
  Sub ASPConnect(oServer, oApplication, oSession, oRequest, oResponse)
   Set Server = oServer
   Set Application = oApplication
   Set Session = oSession
   Set Request = oRequest
   Set Response = oResponse
   cnnState = True
  End Sub

  Sub TestWrite(sHTML)
   Response.Write sHTML
  End Sub

  Sub TestWriteLine(sHTML)
   Response.Write sHTML & "<BR>"
  End Sub
 ]]>

 </script>
 </scriptlet>

傳遞ASP對象的ASP程式如下:
testscp.asp代碼

 <%
 Set scrip=Server.CreateObject("testscp.Scriptlet")
 if Err then
  Response.Write "<BR>Error:" & Err.Number
  Response.Write "<BR>" & Err.Description
  Response.Write "<BR>" & Err.Source
  Response.End
 End If

 Response.Write "<BR> cnnState=" & scrip.cnnState
 if Err then
  Response.Write "<BR>Error:" & Err.Number
  Response.Write "<BR>" & Err.Description
  Response.Write "<BR>" & Err.Source
  Response.End
 End If
  
 Response.Write "<BR>Connecting..."
 scrip.ASPConnect Server, Application, Session, Request, Response
 if Err then
  Response.Write "<BR>Error" & Err.Number
  Response.Write "<BR>" & Err.Description
  Response.Write "<BR>" & Err.Source
  Response.End
 End If
  
 Response.Write "<BR>cnnState=" & scrip.cnnState & "<BR>"
 Response.Write "<BR>Testing ...Write<BR>"
 
 scrip.TestWriteLine ""
 scrip.TestWrite "This is the first part "
 scrip.TestWrite "and this is the second."

 Response.Write "<BR>Testing ...WriteLine<BR>"
 scrip.TestWriteLine "This is text with a line break."
 scrip.TestWriteLine "And so is this."
 %>
上面的代碼沒有包括<implement type = "ASP"/>, 但是仍然可以訪問ASP對象。
因為他們是被當成對象傳遞給scriptlet.
下面再給出一個很常用的取時間的狸子:

scp_dmy.sct
 <?XML version="1.0"?>
 <scriptlet> 

 <?scriptlet error="true" debug="true"?>

 <registration
     description="scp_dmy"
     progid="scpdmy.Scriptlet"
     version="1.00"
     classid="{bba68a50-3ae0-11d3-a9c0-0080c8034244}"
 >
 </registration> 

 <public>    
     <method name = "option_num">
     <PARAMETER name = "num"/>
     </method>
     <method name = "option_month">
     </method>
     <method name = "option_year">
     </method>
 </public>  

 <implements type="ASP" id="ASP"/>  
 <script language="VBScript">

 <![CDATA][
 Sub option_num(num)
     For i = 1 to num
         If i<10 Then
             x = 0 & i
         Else
             x = i
         End If
         Response.write "<option value = " & x & ">" & x & "</option>"
     Next
 End Sub  

 Sub option_month()
     For i = 1 to 12
         If i<10 Then
             x = 0 & i
         Else
             x = i
         End If
         Response.write "<option value = " & x & ">" & MonthName(x,True) & "</option>"
     Next
 End Sub 

 Sub option_year()
     For i = -1 To 3
         yy = year(DateAdd("yyyy",i,Date()))
         Response.write "<option value = " & yy & ">" & yy & "</option>"
      Next
 End Sub
  
 ]]>
 </script>
  
 </scriptlet>

Dmy_check.asp: 檔案
 <%
     Set scrip = Server.CreateObject("scp_dmy.Scriptlet")
 %>

 <html>
 <head></head>
 <body>

 <form name = "form1">
 從 :
 <select name = "D1">
 <option value="">dd</option>
 <% call scrip.option_num(31) %>
 </select>
  
 <select name = "M1">
 <option value="">mm</option>
 <% call scrip.option_month() %>
 </select>

 <select name = "Y1">
 <option value="">yy</option>
 <% call scrip.option_year() %>
 </select>
 <BR><BR>

 到 :
 <select name = "D2">
 <option value="">dd</option>
 <% call scrip.option_num(31) %>
 </select>
  

 <select name = "M2">
 <option value="">mm</option>
 <% call scrip.option_month() %>
 </select>

 <select name = "Y2">
 <option value="">yy</option>
 <% call scrip.option_year() %>
 </select>
 </form>

 <%
     Set scrip = nothing
 %>  

 </body>
 </html>
要記住的是,現在伺服器上所有的程式都能夠使用這個scriptlet,這能夠減少include檔案的使用。
上面的代碼還能夠增加一些功能,例如使用不同的格式顯示日期。
也就是說如果一個運用程式需要顯示dd-mm-yyyy的日期格式,可以在scriptlet中調用各自的函數。

最後,使用scriptlet的還有一個好處就是,它不象真正的ASP組件,因為真正的ASP組件在註冊和取消註冊的
時候都需要你重新啟動IIS,而使用scriptlet是不需要這麼乾的,僅僅是幾句代碼而已。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.