07.12.2001 ASP (Active Server Pages) & ActiveX Reyhan Serim Fikret Sivrikaya
Interactive, Dynamic Web Pages • Allows interaction with user, information retrieval and processing, and conditional output, as opposed to static HTML pages. • Some technologies; Active Server Pages, PHP, CGI Scripting, Cold Fusion, Java Servlets or JSP
What is an Active Server Page? • A text file that resides on the web server • Contains ASP code and also standard HTML tags. • Advantage over standard HTML pages: Dynamic content; pure HTML generated on the server is sent to the browser.
Scripting language code <% .... %> An example Active Server Page <HTML> <BODY> <% For i = 2 To 5 %> <FONT SIZE="<% = i %>">Hello World!<BR> <% Next %> </BODY> </HTML> <HTML> <BODY> <FONT SIZE=“2">Hello World!<BR> <FONT SIZE=“3">Hello World!<BR> <FONT SIZE=“4">Hello World!<BR> <FONT SIZE=“5">Hello World!<BR> </BODY> </HTML>
Scripting Languages • The default (and widely used) scripting language: VBScript (a subset of Visual Basic) • Other alternatives: Jscript and Perl • We will use VBScript in our examples
Requirements to run ASP • Web Server • Microsoft Internet Information Server (IIS) or Personal Web Server (PWS) • Third parties develop ASP add-ins for other web servers • ODBC Drivers • For database interaction (if to be used) • Editor • A simple text editor is fine for development
ASP Programming with VBScript • No need to declare variables (except arrays) • Case Insensitive • Outputting varibale values: <%=var%> <% x = 2 x = “Authors” %> <% Dim arr(5) For i = 0 To 5 arr(i)=0 Next %> <% Do While records.EOF ‘This is a comment %> <B><%=records(“name”)%></B><BR> <% records.MoveNext Loop %> <% If num < 0 Then %> <B>Negative Number</B> <% ElseIf num > 0 Then %> <B>Positive Number</B> <% Else %> <B>Number is 0</B> <% End If %>
ASP Programming with VBScript • Procedures and Functions Subsub_name statement group End Sub Function upperstr (str1) retStr = str1 retStr = Replace(retStr, "i", "İ") retStr = Replace(retStr, "ı", "I") upperstr = UCase(retStr) End Function Functionfunc_name statement group func_name = x End Function
HTML Forms • Asp pages mostly get information from HTML forms <form action=“login.asp" method=“POST"> • When a form is submitted, the browser creates a string consisting of all form filed names and their values -> “queryString” username=demo&password=1234 • “GET” vs “POST” as method POST: send information as part of HTTP header GET: send information as a string to the .asp
How ASP Files Handle Form Data • the “Request” object request(“field_name”) <% If request(“username”)=“demo” && request(“password”)=“1234” then runDemo() End if %>
Maintaining States • Web is stateless; a page is requested by the browser, server send page and the transaction is complete • We may want to keep information through a set of pages (such as username and password) • One method: using hidden fields in HTML forms hard to keep track of; every page must take and pass it to the next page • Alternative: ASP Session object
ASP Session Object • Once created, Sessions variables available for other pages as long as the session is active Session(“username”) = request(“username”) • The session is terminated after 20 minutes (default) of inactivity or by an explicit call to abondon method Session.Abondon() Session.Timeout [ = nMinutes] • Referencing Session variables: if isempty( Session(“username”)) then
Database Interaction • ODBC • Open Database Connectivity; a protocol functioning as an interface to databases for use of applications • A database is registered to ODBC with a DSN (Data Source Name) • ODBC holds the DSN, location, type of databases • For an application to interact with a database, it is enough to know its DSN only • SQL (Sctructured Query Language) is used to query databases
Creating an ODBC Data Source Name • Open Control PanelODBC • Select the System DSN tab • Click the Add… button • You are then prompted for a database driver. Select the correct one for your database (e.g. Microsoft Access Driver (*.mdb)) and Click Finish • Enter a Data Source Name (DSN) for this database (you choose the name). You can also provide a Description (optional) • The next stage is to provide a database Click Select… • Locate your database then click OK. • Once you have provided a DSN and a database, click OK twice.
Connecting to a database using ASP 1. Create an instance of the Connection object Set conn = Server.CreateObject("ADODB.Connection") 2. Open a connection to the database conn.Open "data_source_name" where data_source_name is the DSN as specified in ODBC 3. Create an Instance of the Recordset object Set rsUsers= Server.CreateObject("ADODB.recordset") The recordset (rsUsers) will store the results of the query. It will contain a number of database records depending on the result of the SQL query. 4. Execute the SQL statement and store the result rsUsers.Open query_name,conn query_name is a string variable containing the SQL query
Connecting to a database using ASP An Example Database Connection & Query <% Set conn = Server.CreateObject("ADODB.Connection") conn.Open “members” Query="Select username,password from tblUsers" Set rsUsers = Server.CreateObject("ADODB.recordset") rsUsers.Open Query,conn %>
Processing the query results -the recordset- <HTML> <BODY> <TABLE BORDER=1 CELLPADDING=0> <TR><TD><B>Username</B></TD><TD><B>Password</B></TD></TR> <% Do While Not rsUsers.EOF %> <TR> <TD><%=rsUsers("username")%></TD> <TD><%=rsUsers("password")%></TD> </TR> <% rsUsers.Movenext Loop %> </TABLE> </BODY> </HTML>
<% ‘Open a connection to database Set conn = Server.CreateObject("ADODB.Connection") conn.Open “members” ‘Run query to get the password for the specified username Query="Select password from tblUsers WHERE username=‘“ & request(“username”) & “’” Set rsUsers = Server.CreateObject("ADODB.recordset") rsUsers.Open Query,conn ‘Check username and password If rsUsers.EOF then response.redirect “login_err.asp?type=user” ‘user not found ElseIf rsUsers(“password”) <> request(“password”) then response.redirect “login_err.asp?type=passwd” ‘invalid password Else session(“username”) = request(“username”) ‘login successful response.redirect “members/index.asp” End if rsUser.Close %> Login.asp (an ASP page for login control)
ADVANTAGES Active Server Pages are browser independent. Easy to create and use. Offers choice of scripting languages. No extra software costs. DISADVANTAGES Reliance on Microsoftplatforms. Not a full program development environment, so debugging a script may not bestraightforward. A Review of Advantages & Disadvantages of ASP
References (for ASP) • http://les1.man.ac.uk/is/courses/asp/material.htm A basic -easy to follow- introduction to ASP, examples and other resources (from Univ. of Manchester) • http://msdn.microsoft.com MSDN (Microsoft Developer Network) Online • “ASP in a Nutshell: A Desktop Quick Reference”, 2000 by A. Keyton Weissinger
What is ActiveX A set of technologies that enables software components to interact with one another in a networked environment, regardless of the language in which they were created. ActiveX™ is built on the Component Object Model (COM).
COM: The Fundamental "Object Model" for ActiveX COM(Component Object Model) is the technical cornerstone for the ActiveX technology; it defines how objects expose themselves for use within other objects and how objects can communicate between processes and across a network. COM objects can be easily integrated for use in many languages, such as Java, Basic, and C++. COM objects are reusable binary components.
OLE &COM OLE (Object Linking and Embedding) is a mechanism that allows users to create and edit documents containing items or “objects” created by multiple applications. COM provides the infrastructure used when OLE objects communicate with each other.
ActiveX is an Open Platform for Internet/Intranet Applications
ActiveX on the Server • Microsoft Internet Information Server, which supports CGI applications, ISAPI DLLs, connectivity to databases, and so forth. • Server side scripting, code named Denali. • Transactions, code named Viper. • Message Queuing, code named Falcon.
Why use ActiveX? • ActiveX Controls and Scripting give developers the infrastructure needed to add language- and tool-independent extensions to Web pages • Using ActiveX Controls lets developers take advantage of existing OLE development tools and the investment they have already made in OLE.
Why use ActiveX? (cont.) • ActiveX Scripting allows you to drop any scripting engine into Internet Explorer, enabling developers to add behavior to Web pages in whatever scripting language they prefer • Combines the benefits of the World Wide Web and the personal computer to enable interactive distributed applications and richer communications between users.
Why use ActiveX? (cont.) • ActiveX has also greatly improved extending the HTTP and FTP protocols. The ActiveX encapsulate a new protocol that supports the concept of binding to a URL dynamically from within your application. An application binds to a URL moniker, which then communicates through the appropriate protocol to activate the OLE object.
Microsoft ActiveX supported standards Standards and Technologies Area in ActiveX (partial listing) Networking TCP/IP, IPX/SPX, PPP, SLIP, DNS, RIP, PPTP, DHCP, WINS, IPng, SSL Web HTTP, HTML, VRML Communication RTP, T.120, T.127, H.323, RSVP Messaging SMTP, X.400, MIME, POP3 Databases ODBC (ISO 9942), SQL Programming ANSI C (ISO 9899), ANSI C++, Languages Java, Basic Directory X.500 Miscellaneous FTP, Gopher, Telnet
ActiveX Components An ActiveX component is a unit of executable code, such as an .exe, .dll, or .ocx file, that follows the ActiveX specification for providing objects. ActiveX technology allows programmers to assemble these reusable software components into applications and services.
ActiveX Components • ActiveX DLL • ActiveX Exe • ActiveX Control • ActiveX Document
ActiveX DLL ActiveX objects can exist in the same process as their controller, or in a different process. In-process server objects are implemented in a dynamic-link library (DLL) and are run in the process space of the controller. Since they are contained in a DLL, they cannot be run as stand-alone objects.
ActiveX DLL Example An exe has its own process space Application.exe Dim x as Component1.Widget Set x=New Widget x.Spin ... Application uses component by creating objects from classes the component provides and invoking properties end the methods of the objects Component1.dll Public Sub Spin() Dim y as Component2.Gear Set y =New Gear .... End Sub Component1 Winger class uses Gear class provided by another in process Component2 running the same applications workspace ActiveX DLLs run on the process space of an exe Component2.dll
ActiveX DLL Creation Steps • Determine the features your component will provide. • Determine what objects are required to divide the functionality of the component in a logical fashion. • Design any forms your component will display. • Design the interface — that is, the properties, methods, and events — for each class provided by your component.
ActiveX DLL Creation Steps(cont.) • Create a project group consisting of your component project and a test project. • Implement the forms required by your component. • Implement the interface of each class. • As you add each interface element or feature, add features to your test project to exercise the new functionality. • Compile your DLL and test it with all potential target applications.
ActiveX Exe Out-of-process server objects are implemented in an executable file and are run in a separate process space. Access to in-process objects is much faster than to out-of-process server objects because Automation does not need to make remote procedure calls across the process boundary.
ActiveX Exe Example Uses an out-of-process component by creating objects from the classes component provides The out-of-process component runs in its own process and provides Widget objects Application.exe Dim x as Component1.Widget Set x=New Widget x.Spin ... Component1.exe... Component2.dll Dim x as Component1.Widget Set x=New Widget x.Spin
ActiveX Exe Creation Steps • Determine the features your component will provide. • Determine what objects are required to divide the functionality of the component in a logical fashion. • Design any forms your component will display. • Design the interface — that is, the properties, methods, and events — for each class provided by your component.
ActiveX Exe Creation Steps(cont.) • Create a separate test project, usually a Standard Exe project. • Implement the forms required by your component. • Implement the interface of each class. • As you add each interface element or feature, add features to your test project to exercise the new functionality. • Compile your Exe and test it with all potential target applications.
ActiveX Control An ActiveX control, like a built-in control, is an object that you place on a form to enable or enhance a user's interaction with an application. ActiveX controls have events and can be incorporated into other controls. These controls have an .ocx file name extension.
An ActiveX control that simply uses events A developer purchases ControlDemo.ocx and uses it to create an application Application.exe ... ControlDemo.ocx Private Sub UserControl_Click() End Sub When the user of the application clicks on the control the UserControl object receives an event Any code the outer of the control has placed ın the click event procedure will get executed. The developer doesn't have an event procedure to put the code into
Inserting ActiveX controls into an HTML page Developers can insert ActiveX controls into a Web page by using the <OBJECT> tag in the HTML page and referring to the ClassId of the ActiveX control.
ActiveX Control Creation Steps • Determine the features your control will provide. • Design the appearance of your control. • Design the interface for your control — that is, the properties, methods, and events your control will expose. • Create a project group consisting of your control project and a test project. • Implement the appearance of your control by adding controls and/or code to the UserControl object.
ActiveX Control Creation Steps(cont.) • Implement the interface and features of your control. • As you add each interface element or feature, add features to your test project to exercise the new functionality. • Design and implement property pages for your control. • Compile your control component (.ocx file) and test it with all potential target applications.
ActiveX Document ActiveX documents are forms that can appear within Internet browser windows. Visual Basic ActiveX documents offer built-in viewport scrolling, Hyperlinks, and menu negotiation. ActiveX documents are designed the same as Visual Basic forms. They can contain insertable objects, such as Microsoft Excel pivot tables. They can also show message boxes and secondary forms.
ActiveX Document Creation Steps • Determine the features your document will provide. • Design the appearance of your document. • Design the interface for your document; that is, the properties, methods, and events your document will expose. • Create a project consisting of your user document and any auxiliary forms.
ActiveX Document Creation Steps(cont.) • Add controls and/or code to the UserDocument object. • Implement the interface and features of your document. • Compile your document to create a .vbd file and test it with all potential target applications.
References (for ActiveX) • “ActiveX Programming Unleashed”, Weiying Chen Free online book available for download at http://www.developer.com • http://msdn.microsoft.com MSDN (Microsoft Developer Network) Online • “Microsoft Visul Basic, Books Online”