Some application of XML in ASP

Source: Internet
Author: User
Tags add date object cdata end include sql version
XML is used to define objects, methods, attributes, and so on using scripts and components written in Extensible Flag Language (XML) and. Xml.
The script also provides a function. Scripting languages can be JavaScript, VBScript, and so on. The use of the site's application procedures and the general example of the site, can now be converted into scriptlets. As long as you use scriptlets to write components, they can be reused in the Web site or use the various parts of the program, can be used on the server (such as ASP code) also can be used in the client (such as browser).

Here we will focus on the server-side scriptlets for component ASP code.
Earlier, ASP developers were accustomed to writing include files. Used to do public service-side routines,
Also used in their website application program. Functionally, include files are limited to simply referencing them in the page.
Completes the function of passing parameters.
The same common routines are now able to add scriptlet methods and help with the use of components in the application program.
This method is now widely used in the Web site application program, can be run on the same server application reuse.
This approach makes it very easy to get (Pull-down) data out of the database.

Example: Establish scriptlet methods and attributes that make it very popular to get data from a database.
And make all the applications running on the same server use the same scriptlet without having to write code again.

The code is as follows:
Scriptlet file name is 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 ' > No records found. </option> "
Else
Do as 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>
Specific descriptions of the various elements used in the scriptlets can be found at http://msdn.microsoft.com/scripting.
Here's how to specifically invoke Scriptlets code: (This code can be used as long as the scriptlets application is registered on the same server)
query2list.asp file:
<%@ 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 ")
%>
<body>
<center>
<form name = "FR1" >
<select>
<option value= "None" selected> Select publisher </option>
<% Call Objscpquery2list.query2list (objconn, SQL)%>
</select>
</form>
</center>
</body>

<%
Objconn.close
Set objconn = Nothing
Set objscpquery2list = Nothing
%>

The problems that arise:
The first use of scriptlets is likely to be the problem, is the memory waste phenomenon, the improvement method is as follows:
The ASP object is passed to the scriptlets instead of using the ASP interface. (Just don't use <implements type= "ASP" id= "ASP"/> Affirm)
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>

The ASP program that passes the ASP object is as follows:
Testscp.asp Code

<%
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"
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 are this."
%>
The code above does not include the <implement type = "ASP"/&gt, but the ASP object can still be accessed.
Because they are passed to scriptlet as objects.
The following is a very common time to take the Beaver Son:

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: File
<%
Set scrip = Server.CreateObject ("Scp_dmy. Scriptlet ")
%>

<body>

<form name = "Form1" >
From:
<select name = "D1" >
<option value= "" >dd</option>
<% Call Scrip.option_num%>
</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>

To:
<select name = "D2" >
<option value= "" >dd</option>
<% Call Scrip.option_num%>
</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>
Keep in mind that all programs on the server are now able to use this scriptlet, which can reduce the use of include files.
The above code can also add features, such as displaying dates in different formats.
That is to say, if an application needs to display the DD-MM-YYYY date format, it can call its own function in Scriptlet.

Finally, one of the benefits of using scriptlet is that it's not like a real ASP component, because the real ASP component is registering and canceling the
You need to restart IIS, and using Scriptlet is not necessary, just a few lines of code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.