翻譯得很蹩腳,望大家諒解,呵呵。 Scriptlets - 一個更好的組織你的ASP代碼的方法
使用可擴展標志語言(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是不需要這么干的,僅僅是幾句代碼而已。 (出處:熱點網絡)
|