XML在ASP中的一些运用

王朝asp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

使用可扩展标志语言(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是不需要这么干的,仅仅是几句代码而已。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航