Download presentation
Presentation is loading. Please wait.
1
Active Server Pages Overview
Introduction COM and ASP Database Access ASP Access to other Apps Favorite Sites to Visit 1/13/2019 Dr. Paul Bao, COMP035
2
Introduction Microsoft® Active Server Pages (ASP) is a server-side scripting technology used to create dynamic and interactive Web applications. An ASP page is an HTML page containing server-side scripts processed by the Web server Server-side scripts run when a browser requests an .asp file from the Web server. ASP is called by the Web server, which processes the requested file from top to bottom and executes any script commands. It then formats a standard Web page and sends it to the browser. 1/13/2019 Dr. Paul Bao, COMP035
3
Built-in Objects Request - to get information from the user
Response - to send information to the user Server - to control the Internet Information Server Session - to store information about and change settings for the user's current Web-server session Application - to share application-level information and control settings for the lifetime of the application Others - more built-in objects have been introduced in IIS 4 1/13/2019 Dr. Paul Bao, COMP035
4
Request & Response Objects
-<html> <head> <title>Who Are You?</title> </head><body> <p><%= formatdatetime((now),vblongdate) %></p> <p>Thank You For Filling Out the Form - <% Response.Write Request.Form("name") %></p> <p>You are <%= Request.Form("age") %> Years of Age</p> <p>Your shoe size is <%= Request.Form("size") %> <% If Request.Form("size") > 12 then Response.Write " You have big feet" End If %></p> <p>Your Bestest Color is: <%= Request.Form("color") %> </body> </html> 1/13/2019 Dr. Paul Bao, COMP035
5
Server Objects CreateObject() method creates an instance of a server component. Syntax Server.CreateObject( progID ) Parameters progID Specifies the type of object to create. The format for progID is [Vendor.]Component[.Version]. <% Set MyAd = Server.CreateObject("MSWC.AdRotator") %> 1/13/2019 Dr. Paul Bao, COMP035
6
Server Objects Compenents
Ad Rotator - Automatically rotates advertisements displayed on a page according to a specified schedule. Browser Capabilities - Determines the capabilities, type, and version of each browser that accesses your Web site. Database Access - Provides access to databases. Content Linking - Creates tables of contents for Web pages, and links them together sequentially like pages in a book. File Access - Provides access to file input and output. PageCounter - Provide page counter 1/13/2019 Dr. Paul Bao, COMP035
7
Session Objects <% Session("username") = "Janine"
You can use the Session object to store information needed for a particular user-session. Variables stored in the Session object are not discarded when the user jumps between pages in the application <% Session("username") = "Janine" Session("age") = 24%> <% Set Session("Obj1") = Server.CreateObject("MyComponent") %> You can then call the methods and properties of MyObj on subsequent Web pages, by using the following: <% Session("Obj1").MyObjMethod %> 1/13/2019 Dr. Paul Bao, COMP035
8
Server Compenent Example
<% Set pageCount = Server.CreateObject("MSWC.PageCounter") %> <% pageCount.PageHit %> You are visitor number <% =pageCount.Hits %> to this Web site. 1/13/2019 Dr. Paul Bao, COMP035
9
Application Objects You can store values in the Application object. Information stored in the Application object is available throughout the application and has application scope. <% Application("greeting") = "Welcome to My Web World!" Application("num") = 25%> <% Set Application("Obj1") = Server.CreateObject("MyComponent") %> You can then reference the methods and properties of MyObj on subsequent Web pages, by using the following: <% Application("Obj1").MyObjMethod %> 1/13/2019 Dr. Paul Bao, COMP035
10
COM Objects It is possible to extend your ASP scripts using COM components COM extends your scripting capabilities by providing a compact, reusable, and secure means of gaining access to information. You can call components from any script or programming language that supports Automation. 1/13/2019 Dr. Paul Bao, COMP035
11
How to Use ASP Write and Run an ASP Page.
Describes how to use Visual Basic® Scripting Edition (VBScript) and HTML tags. Send Information Using Forms. Shows how to display forms on an HTML page. Create a Guest Book. Uses forms to gather information from visitors, store the information in a database, and display the database contents in a Web page. Display an Excel Spreadsheet in ASP. Explains how to display an Excel spreadsheet in a Web page. 1/13/2019 Dr. Paul Bao, COMP035
12
Write and Run an ASP Page
VBScript syntax and coding samples To create an ASP page, use a text editor to insert script commands into an HTML page. Saving the page with an .asp file name extension. To view the results of a script, request the page using a Web browser. VBScript is the default scripting language for ASP 1/13/2019 Dr. Paul Bao, COMP035
13
Example I <%@ Language=VBScript %> <html> <head>
<title>Example 1</title> </head> <body> <%FirstVar = "Hello world!" %> <%=FirstVar%> </body> </html> 1/13/2019 Dr. Paul Bao, COMP035
14
Example I Assigns the text "Hello World" to the variable FirstVar.
Uses HTML to make an HTML page. Uses <%FirstVar%> to print out the value of the variable FirstVar. Ends the HTML page. 1/13/2019 Dr. Paul Bao, COMP035
15
Example 2 This example incorporates a FOR loop in the ASP page.
Language=VBScript %> <html><head> <title>Example 2</title> </head> <body> <%FirstVar = "Hello world!"%> <%FOR i=1 TO 10%> <%=FirstVar%> <%NEXT%> </body></html> 1/13/2019 Dr. Paul Bao, COMP035
16
Example 3 In this example, a time stamp is added to the ASP page.
The word time is a predefined VBScript function variable containing the current time. There are more than 90 functions in VBScript. 1/13/2019 Dr. Paul Bao, COMP035
17
Example 3 <%@ Language=VBScript %> <html><head>
<title>Example 3</title> </head> <body> <%FirstVar = "Hello world!"%> The time is: <%=time%> <BR> <%FOR i=1 TO 10%> <%=FirstVar%> <%NEXT%> </body> </html> 1/13/2019 Dr. Paul Bao, COMP035
18
Example 4 (If statement)
Language=VBScript %> <html><head> <title>Example 4</title> </head> <body> <%IF Hour(time)>18 OR Hour(time)<4 THEN%> Good Night Everyone. <%ELSE%> Good Morning Everyone. <%END IF%> </body> </html> 1/13/2019 Dr. Paul Bao, COMP035
19
Send Information by Using Forms
With ASP, you can embed scripts written in VBScript directly into an HTML file to process the form. ASP processes the script commands and returns the results to the browser. Create an HTML page that displays various elements of an HTML form. 1/13/2019 Dr. Paul Bao, COMP035
20
Input.html <html><head>
<title>Who Are You?</title> </head><body> <p>Please tell us who you are</p> <form action="form.asp" method="post"> <p>Your Name : <input type="text" name="name"> </p> <p>Your Ages : <input type="text" name="age"></p> <p>Shoe Size : <input type="text" name="size"></p> <p>Your Bestest Color : <select name="color" SIZE=3> <option selected> Red <option> Blue <option> Green </select></p> <p><input type="submit"></p> </form></body></html> 1/13/2019 Dr. Paul Bao, COMP035
21
Form.asp <html><head>
<title>Who Are You?</title> </head> <body> <p>Thank You For Filling Out the Form - <% Response.Write Request.Form("name") %></p> <p>You are <%= Request.Form("age") %> Years of Age</p> <p>Your shoe size is <%= Request.Form("size") %> <%If Request.Form("size") > 12 then Response.Write " You have big feet" End If %></p> <p>Your Bestest Color is: <%= Request.Form("color") %> </body> </html> 1/13/2019 Dr. Paul Bao, COMP035
22
Create a Guest Book How to develop a guest book application.
Guest books allow visitors to your site a chance to give you feedback. Information such as the visitor’s name, address, and comments can be available to you. 1/13/2019 Dr. Paul Bao, COMP035
23
Creating the Guest Book Database
You must first create an Access database called Guestbook.mdb. The database must have the fields with the properties described in the following table. 1/13/2019 Dr. Paul Bao, COMP035
24
Create DSN Create a data source name (DSN) connection to the database so your ASP application can interact with it. How to create a DSN on Windows NT and Windows 2000 In the ODBC Data Source Administrator, select the ODBC icon. Select File DSN. Select Add, select Microsoft Access Driver, and click Next. Type in a descriptive name for your file DSN (Guestbook) and click Next. Click Finish, click Select, specify the location of the database file, and select OK. Click OK twice. After you specify the location of the database file, the ODBC Data Source Administrator creates a file DSN for it. 1/13/2019 Dr. Paul Bao, COMP035
25
Coonection to Database File
DSN-less set objConn = server.createobject("ADODB.Connection") 'Opens the connection to the data store mdbfile=Server.MapPath("Guestbook.mdb") objConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & mdbfile & ";” DSN strProvider="DSN=guestbook.dsn;" objConn.Open strProvider 1/13/2019 Dr. Paul Bao, COMP035
26
Guestbook.asp Demo Guestbook.asp 1/13/2019 Dr. Paul Bao, COMP035
27
View Database in a Browser
Demo viewdb.asp 1/13/2019 Dr. Paul Bao, COMP035
28
Display an Excel Spreadsheet in ASP
Display a Microsoft Excel spreadsheet in a Web page. Use ActiveX® Data Objects (ADO) component, which is used as a connection mechanism to provide access to data. How to view and edit a spreadsheet with a Web browser using ADO 1/13/2019 Dr. Paul Bao, COMP035
29
Prepare Excel spreadsheet
Create a spreadsheet as ASPTOC.xls in the your NT directory. Highlight the rows and columns on the spreadsheet that you want displayed in the Web page. On the Insert menu, choose Name, and select Define. If there are any names listed, select them and select Delete. Type a name for the workbook, select Add, and select OK. To display the spreadsheet in a Web page, you must create a DSN for the spreadsheet Demo asptol.asp 1/13/2019 Dr. Paul Bao, COMP035
30
Redirect Users from Ad Links
When a user clicks the ad, the browser appends a query string to the request to the server. Then, the server directs the user’s browser to the ad’s URL. Adrotatorredirect.asp: %> <html><head> <title>Redirection Page</title> </head> <body> <%'Set the response buffer on Response.Buffer = True Dim lsURL 'Obtain the URL from the query string lsURL = Request.QueryString("URL") 'Clear the response and redirect to URL Response.Clear() Response.Redirect(lsURL)%> </body></html> 1/13/2019 Dr. Paul Bao, COMP035
31
Count Page Hits The Page Counter component uses an internal object to record page hit-count totals for all pages on the server. At regular intervals, this object saves all information to a text file so that no counts are lost due to power loss or system failure. The Page Counter component uses the following three methods: Hits(). This displays the number of hits for a Web page. The default is the current page. PageHit(). This increments the hit count for the current page. Reset(). This resets the hit count for a page to zero. The default is the current page. 1/13/2019 Dr. Paul Bao, COMP035
32
Pagehit.asp <% Set pageCount = Server.CreateObject("MSWC.PageCounter") %> <% pageCount.PageHit %> You are visitor number <% =pageCount.Hits %> to this Web site. 1/13/2019 Dr. Paul Bao, COMP035
33
Remote Server Scripting
Have you ever wanted to write an ASP page that did the following: execute an ASP script on another webserver retrieve that remote ASP script's output act on that output? Or perhaps you've always wanted to be able to do remote Server Side Includes, like <!--#include file=" ASP, alone, can't do that sort of thing. There is a free component, AspTear, from Softwing 1/13/2019 Dr. Paul Bao, COMP035
34
Remote Stock Quote Let's say that you want to execute an ASP script on another server that returns the current stock price for Microsoft. We could write our own ASP script on our own server that would show this value 1/13/2019 Dr. Paul Bao, COMP035
35
Stock.asp 'AspTear constants Const Request_POST = 1 Const Request_GET = 2 Set objTear = CreateObject("SOFTWING.ASPtear") Response.ContentType = "text/html" On Error Resume Next Dim strRetval ' URL, action, payload, username, password strRetval = objTear.Retrieve(" Request_GET, "", "", "") If Err.Number <> 0 Then Response.Write "<b>" If Err.Number >= 400 Then Response.Write "Server returned error: " & Err.Number Else Response.Write "Component/WinInet error: " & Err.Description End If Response.Write "</b>" Response.End End If Response.Write "Microsoft currently selling at " & FormatCurrency(strRetval, 2) 1/13/2019 Dr. Paul Bao, COMP035
36
Remote Quote of Any Stock
let's say that there was an ASP page that would return any stock quote. All you needed to do was specify the stock symbol in the QueryString. Let's look at how the code for that might look: 1/13/2019 Dr. Paul Bao, COMP035
37
AnyStock.asp 'AspTear constants Const Request_POST = 1 Const Request_GET = 2 Set objTear = CreateObject("SOFTWING.ASPtear") Response.ContentType = "text/html" On Error Resume Next Dim strRetval ' URL, action, payload, username, password strRetval = objTear.Retrieve(" Request_GET, "symbol=MSFT", "", "") ' ... Error checking code omitted Response.Write "Microsoft currently selling at " & FormatCurrency(strRetval, 2) 1/13/2019 Dr. Paul Bao, COMP035
38
Favorite Links to Visit
1/13/2019 Dr. Paul Bao, COMP035
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.