Title: ASP Components with Scripting Languages
1ASP Components with Scripting Languages
- The Windows Script Component technology helps to
write script-based COM components to access ASP
objects - Dino Esposito
- desposito_at_vb2themax.com
2Agenda
- Windows Script Components (WSC)
- Writing WSC components
- ASP-specific WSC components
- Examples
- A Query Maker
- A Recordset-to-Table Converter
3Windows Script Components
- Technical Overview
- What are WSC components
- Quick Tour of the WSC Architecture
- Languages and tools involved
WSCs are COM objects written with scripting
languages.
4COM Objects with Script?
A few (reasonable) objections
- A COM object is a binary file. What has that to
do with VBScript or JScript? - A COM object has a well-known binary layout. Who
provides that for a script file? - A COM object exposes interfaces. How is that
possible with a scripting language? - A COM object needs registration and a CLSID. Who
deals with that?
5WSC At a Glance
- WSC components were first introduced a couple of
years ago as a superset of DHTML Scriptlets. - They are regular COM objects.
- WSC components can be seen as 3-tiered components
glued together by XML code. - Descriptive Metadata (XML tags)
- Script code providing the expected behavior
- The machinery that makes it work the COM way
6Demo
- The worlds simplest WSC component.
- Written in VBScript with just one method to say
Hello
7Structure of a WSC
ltcomponentgt ltregistration progid"HelloWorld.WSC"
/gt ltpublicgt ltmethod name"Welcome"/gt lt/publicgt lt
script language"VBScript"gt Function
Welcome MsgBox "Hello, World" End
Function lt/scriptgt lt/componentgt
8Agenda
- Windows Script Components (WSC)
- Writing WSC components
- ASP-specific WSC components
- Examples
- A Query Maker
- A Recordset-to-Table Converter
9Descriptive Tier
Set oTL CreateObject( _
Scriptlet.TypeLib) oTL.AddURL
DB2HTML.wsc oTL.Path DB2HTML.tlb" oTL.Doc
DB/HTML Converter oTL.Name Db2HtmlTLB" oTL.Wr
ite oTL.Reset
- The ltregistrationgt tag provides info about the
name of the object, its CLSID and can include
code that executes upon (un)registration.
ltregistration descriptionDB/HTML
Converter lt!-- optional --gt progidDB2HTML.WSC"
version"1.00 lt!-- optional
--gt classid"e8c35060-1879-11d3-b17c-00c0dfe3973
6" gt ltscript languageVBScriptgt Function
Register Do something more useful here
MsgBox Registering (e.g. generate a
type-library on-the-fly) End Function lt/scriptgt lt/
registrationgt
10The Public Interface
- The descriptive tier also includes a ltpublicgt
tag. It defines the public programming
interface of the component. In other words, the
methods, properties and events you can call.
You can also assign only get or put or nothing.
ltevent nameOnRecordFound"gt ltparameter
namerecNum/gt lt/eventgt
ltpublicgt ltproperty nameConnection"gt ltget/gt
ltput/gt lt/propertygt ltmethod nameExecute"gt ltpa
rameter namesqlQuery/gt lt/methodgt lt/publicgt
11The ltImplementsgt
- In the world of scripting the main COM interface
is IDispatch, the Automation interface. - When writing a script-based COM object, you may
have other interfaces. How do you implement them?
- The ltImplementsgt tag hides special binary
modules that interpret the XML and script code
you write and transforms it into valid COM code
for clients.
12The Run-time tier
- A binary run-time module (scrobj.dll) takes care
of exposing regular COM interfaces to the
callers. The behavior of these interfaces is then
provided by the script code.
13Interfaces and WSC
- The run-time module implements a fixed number of
interfaces. - Automation and Events ltpublicgt tag
- ASP and Behavior ltimplementsgt tag
- The ltPublicgt tag hides the details of the
Automation interface and the connection pointer
interfaces for events
14Available Interfaces
- Aside from the event and automation interfaces
hidden by the ltpublicgt tag, a WSC can implement - ASP interfaces to access ASP objects
- (IE5) Behavior interfaces
15Agenda
- Windows Script Components (WSC)
- Writing WSC components
- ASP-specific WSC components
- Example
- A Query Maker
- A Recordset-to-Table Converter
16Why (Script) ASP Components?
- Divide et Impera
- Isolate frequently used pieces of script code.
- CreateObject vs ltOBJECT runatServergt
- Wrap once, Reuse forever
- Use with pages that do the same task.
- Reuse existing business logic from legacy code
- Leverage the power of components
- Exploit objects opposite to SSI
17ASP Components
- An ASP WSC component includes
- ltimplements typeASP/gt
- ltimplementsgt is placed at the same level as
ltpublicgt and is not a replacement for it. It
simply makes the ASP objects accessible - The script utilizes the same namespace as the
calling ASP page.
18The Script tier
- A typical ASP Component exposes methods and
properties that are callable from within the
page. - For this reason it needs a ltpublicgt tag to
publish the programming interface. - All the code needed goes in a ltscriptgt section
and can be a mix of VBScript and JScript.
19Agenda
- Windows Script Components (WSC)
- Writing WSC components
- ASP-specific WSC components
- Examples
- A Query Maker
- A Recordset-to-Table Converter
20Demo Query Maker
- This example shows how to ask a component to
query a recordset saving you from typing all the
ADO code. - It works much like the RDS Data Factory object
Set obj Server.CreateObject("QueryMaker.WSC") ob
j.Connection "Contacts" Set rs
obj.GetRecords("select from Contacts")
21Agenda
- Windows Script Components (WSC)
- Writing WSC components
- ASP-specific WSC components
- Examples
- A Query Maker
- A Recordset-to-Table Converter
22Demo Rs2Table
- This example shows how to format HTML tables on
the fly. The content of the table comes from a
recordset. Use special name conventions.
Set oFmtr CreateObject("RsFormatter.WSC") oFmtr.
SetRS (rs) oFmtr.Title "lth2gtList of
contactslt/h2gt" oFmtr.TableStyle
"globTab" oFmtr.AddColumn "", "N. _RECNO_",
"num" oFmtr.AddColumn "Name", "FIRSTNAME
ltbgtLASTNAMElt/bgt", "" oFmtr.AddColumn
"Company", "COMPANY", "company" oFmtr.AddColumn
"Email", "EMAILADDRESS", "" oFmtr.GetText()
23VB and VC
- Script-based ASP components are functionally
equivalent to those written with VB or VC. - They dont have the same performance.
- They dont let you do the same things. Say,
calling Win32 API functions. - They dont rely on a fully-fledged language
- They are far easier to write and deploy.
24When to Use Which?
- Use VC Components for
- Critical components where performance is vital.
- Use VB Components for
- Any reusable component of a significant
difficulty. - Use WSC Components
- For small, unexpensive, reusable pieces of script.
25XML Compliance
- A WSC is a XML file subject to the syntax rules
of XML. - Endeavour to write XML 1.0 compliant code,
whenever possible. - lt?xml version1.0 ?gt
- Writing XML compliant code requires you to
enclose your script code in a couple of CDATA
delimiters
ltscript language"VBScript"gt lt!CDATA
script code here gt lt/scriptgt
26Tools for WSC
- To write WSC components quickly and efficiently,
use the Microsoft Scriptlets Wizard available
from - http//msdn.microsoft.com/scripting
27References
- Windows Script Components
- Cutting Edge, http//www.microsoft.com/mind
- http//msdn.microsoft.com/scripting
- Windows Script Host Prog. Ref. - Wrox Press
- ASP Components
- http//www.asptoday.com
- Professional ASP 3.0 - Wrox Press
28Q A