1 / 156

Active Server Pages

Active Server Pages. Minder Chen, Ph.D. mchen@gmu.edu. Course Description.

halen
Download Presentation

Active Server Pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Active Server Pages Minder Chen, Ph.D. mchen@gmu.edu

  2. Course Description Active Server Pages (ASP) are scripting files processed by Microsoft Internet Information Server. ASP provides server-side scripting to create interactive Web applications, including tasks such as gathering information from the client, updating or retrieving data in a database, or dynamically creating HTML pages returned to the users. This course will describe the basics of Visual Basic Scripting (VB Script) language and how to build ASP using VB Script. Student will learn how to: describe the HTTP protocol; build interactive Web applications; use the ASP objects including Response, Request, Session, and Application objects; create and use cookies; use an ActiveX server component in Web applications. Students will also learn how to create Web pages that access information in a database by using the ActiveX Data Objects (ADO).

  3. Outline • Introduction and architecture • VB Script for Active Server Page • Form Data Processing • Using Build-in and Installable ASP Components • Using Database Access Component (ADO) • Session Management • More Examples (Optional)

  4. Resources • Web Master Training Web Site: • http://65.168.116.6/web9/net • ASP Documentation from Microsoft: • http://msdn.microsoft.com/library/ • Personal Web Server (PWS) is one of many new features available through the Windows NT Option Pack. • http://www.microsoft.com/ntserver/nts/downloads/recommended/NT4OptPk/default.asp • http://www.microsoft.com/windows/ie/pws/ • http://www.windows.com/windows2000/en/server/iis/htm/asp/iiwauslw.htm • Best Web Sites: • http://www.aspin.com/ • http://www.activeserverpages.com/ • http://www.asp101.com • Additional ASP Resources • http://www.microsoft.com/net • http://www.aspcode.net/freeasphost/freeasphost.asp (Free ASP hosting) • http://www.datareturn.com/developerresources.asp • http://msdn.microsoft.com/scripting/ • http://www.chilisoft.com/ ' for porting Asp to other platform • Books: • Beginning Active Server Pages 3.0, Wrox Press Inc. • Professional Active Server Pages 2.0, Wrox Press Inc. • Active Server Pages 3.0, Nicholas Chase, Que, 2000. • ASP in a Nutshell: A Quick Reference, O'Reilly, 1999

  5. Creating ASP Page • ASP uses the delimiters <% and %> to enclose script commands. • By default, the primary scripting language is VBScript. test.asp <HTML> <BODY> This page was last refreshed on <% =Now() %>. </BODY> </HTML> • The VBScript function Now returns the current date and time. <HTML> <BODY> This page was last refreshed on 9/11/98 4:30:00 PM. </BODY> </HTML> Rendered via a browser Interpreted ASP code: An HTML document. This page was last refreshed on 9/11/98 4:30:00 PM.

  6. The Active Server Pages Model Request an ASP file Active Server Page Web Server (IIS) Http://www.xxx.com/path/xxx.asp .asp files ASP Interpreter: Process ASP Scripting Statements Return a processed ASP file in the format of an HTML document Browser • An ASP script begins to run when a browser requests a .asp file from your Web server. • Your Web server then calls ASP, which reads through the requested file from top to bottom, executes any ASP statements, and sends an HTML page to the browser.

  7. ASP Architecture (ADO)

  8. Platforms for VBScript Program • When initiating and processing Web data takes place on the client computer, you can include VBScript code in HTML files to enhance Web pages in Internet Explorer. • When initiating and processing Web data takes place on the server, you can include VBScript code in HTML files that are used by Internet Information Server (IIS) Active Server Pages. • To run scripts directly on the Windows desktop or command console, use Windows Scripting Host (WSH) to run VBScript programs. Server-Side VBScripting Client-Side VBScripting

  9. Active Server Page • A .asp file is an ASCII text file that contains • HTML commands • text or other content (such as client side scripts) • server-side script commands • When a client browser requests a URL with an .asp file name extension, any script contained within the <% and %> delimiters and within <SCRIPT RUNAT=Server LANGUAGE=VBSCRIPT> and </SCRIPT> tags is executed on the server. Use the value JSCRIPT for LANGUAGE for Jscript. • The resulting HTML and text are sent back to the client browser.

  10. Benefits of ASP Applications • Completely integrated with your HTML files. • Easy to create, with no manual compiling or linking of programs required. • Object-oriented and extensible with ActiveX server components. • ASP supplies scripting engines for Microsoft® Visual Basic® Scripting Edition (VBScript) and JScript. • You can incorporate sophisticated functionality using ActiveX server components, formerly known as Automation servers, to process data and generate useful information. • ASP-generated content is compatible with standard Web browsers.

  11. ASP Scripting • The first line in an .asp file specifies the scripting language for the page. For example, the following first line in .asp file specifies that the script is VBScript: <%@ LANGUAGE=VBScript %> • Without a language tag, script in the file is processed as the default language (VBScript by default.) under the ASP entry in the Web server registry. • Active Server Pages can provide a scripting environment for a number of other scripting languages, including Jscript (build-in), REXX, and Perl, and others.

  12. Developing ASP Scripts Creating/Revising ASP scripts Posting ASP scripts on the web server or saving them on the local web server Testing ASP Scripts

  13. Hello.asp: Display Information <HTML><HEAD><TITLE>Hello World Example</TITLE></HEAD><BODY><%Response.write "Hello World!"%> </BODY></HTML> Returned HTML Document <HTML><HEAD><TITLE>Hello World Example</TITLE></HEAD><BODY>Hello World! </BODY></HTML> The start of the script section method Argument Build-in Object The end of script section

  14. Setting Up ASP Files • An Active Server Pages (ASP) file is a text file with the extension .asp that contains any combination of the following: • Text • HTML tags • ASP script commands • Call to ActiveX server components • ASP is server-side scripting. • ASP files only work with the IIS on NT or PWS on Windows 95. • .asp files should be saved on your Web site in a directory that has Script or Execute permission enabled.

  15. http://www.ipswitch.com/ Ws_FTP 65.168.115.6

  16. Using ftp and Testing ASP applications • MkDir to create a new subdirectory (e.g., 01) • Double click a subdirectory to enter to the subdirectory • Test it by using the URL: http://ft-commerce/asp/01/hello.asp • For the server at the lab remotely: http://65.168.115.6/asp/01/hello.asp • For a local server: http://localhost/asp/hello.asp

  17. Hello2.asp <HTML><HEAD> <TITLE>HELLO WORLD</TITLE> </HEAD><BODY> <% FOR i = 3 to 7 %> <FONT SIZE = <% =i %>> Hello World!<BR> <% NEXT %> </BODY></HTML> Return HTML Source Code <HTML><HEAD> <TITLE>HELLO WORLD</TITLE> </HEAD><BODY> <FONT SIZE = 3> Hello World!<BR> <FONT SIZE = 4> Hello World!<BR> <FONT SIZE = 5> Hello World!<BR> <FONT SIZE = 6> Hello World!<BR> <FONT SIZE = 7> Hello World!<BR> </BODY></HTML> http://localhost/asp/hello.asp

  18. Greeting.asp <% ' This is the greeting script If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then Greeting = "Good Morning!" Else Greeting = "Hello!" End If %> <%= Greeting %> • The <%= Greeting %> command sends the current value of the variable to the browser. <% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then %> Good Morning! <% Else %> Hello! <% End If %>

  19. Alternative Approach <% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then Response.Write "Good Morning!" Else Response.Write "Hello!" End If %> • Response.Write sends the text that follows it to the browser. Use Response.Write from within a statement when you want to dynamically construct the text returned to the browser.

  20. Markup • HTML tags are differentiated from text by delimiters: The less than (<) and greater than (>) symbols. • ASP use delimiters <% and %> to enclose script commands. • <% color = "green" %> assigns the value green to the variable color. • The ASP output directive <%= expression %> displays the value of an expression. • the output expression <% = greeting %> sends the current value of the variable greeting to the browser. • The ASP processing directive <%@ keyword %> gives ASP information it needs to process an .asp file. • <%@ LANGUAGE=VBScript %>

  21. Hello3.asp <% ' Define two variables with string values. x = "Hello" y = "World" %> <P>My response is to say "<%= y %>&nbsp; <%= x %>." </P> <% Color = "Green" %> <%Color="Green"%> <% Color = "Green" %> Same result

  22. Hello4.htm: Sample JavaScript Code <HTML><HEAD><TITLE>HELLO WORLD</TITLE></HEAD> <BODY> <% Response.write("Hello world! -- From ASP") %> <SCRIPT LANGUAGE="JavaScript1.2"> document.write("<P>Hello world! -- From Client-Side Scripting!") </SCRIPT> <P>Hello World! -- From HTML </BODY> </HTML>

  23. Data Types • Variables: Simple variables and Array variables • VBScript subsumes all categories of data under one name called a Variant. • At a basic level, Variants contain either string or numeric data. • String data is used for text, while numeric data contains only numbers. • Variant data can be further classified into subtypes. For example, you can have numeric data that represents currency, or a date or time, and the Variant will interpret the data accordingly. • You can use the data type conversion function for data type conversion. For example, CInt function to force conversion of an expression to the Variant of subtype Integer.

  24. Data Types • Empty • Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables. • Null • Variant intentionally contains no valid data. • Boolean • Contains either True or False. • Byte • Contains integer in the range 0 to 255. • Integer • Contains integer in the range -32,768 to 32,767. • Currency • -922,337,203,685,477.5808 to 922,337,203,685,477.5807. • Long • Contains integer in the range -2,147,483,648 to 2,147,483,647.

  25. Data Types • Single • Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values and 1.401298E-45 to 3.402823E38 for positive values. • Double • Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values and 4.94065645841247E-324 to 1.79769313486232E308 for positive values. • Date (Time) • Contains a number that represents a date or time between January 1, 100 to December 31, 9999. • String • Contains a variable-length string that can be up to approximately two billion characters in length. • Object • Contains an object. • Error • Contains an error number.

  26. Declaring Variables • VBScript implicitly creates a variable the first time that it encounters an unrecognized string of characters that could be a variable name. • The Option Explicit statement informs VBScript to generate an error if it encounters an undeclared variable. The Option Explicit statement should be the first line of code in a script that uses variables. • Dimvarname[([subscripts])][, varname[([subscripts])]] . . . • Dim a, x(10), y(2, 5) • A variable name: • Must begin with an alphabetic character. • Cannot contain an embedded period. • Must not exceed 255 characters. • Must be unique. • Is not case-sensitive. • Constants: • Const CorpName = "Volcano Coffee Company" • Const HousePayment = 1500

  27. Assigning Value to a Variable • C = 2 • A = C+300 • UserName = "Bob" • UserName = Bob • CutOffDate = #11-21-98# • Dim A(10) A(0) = 256A(1) = 324A(2) = 100. . .A(10) = 55 C = A(1) Dim MyVar, MyCheck MyCheck = IsEmpty(MyVar) ' Returns True. MyVar = Null ' Assign Null. MyCheck = IsEmpty(MyVar) ' Returns False. MyVar = Empty ' Assign Empty. MyCheck = IsEmpty(MyVar) ' Returns True. <% Option Explicit ' Force explicit variable declaration. Dim MyVar ' Declare variable. MyInt = 10 ' Undeclared variable generates error. MyVar = 10 ' Declared variable does not generate error. %>

  28. Exercise: Variable.asp <% Option Explicit %> <html><head><title>Variables</title><head> <body> <% Dim x, y, x1, a, b, c x = 1 y = 2 response.write x+y x1=6 response.write x1 & "<br>" a =5 b=7 c= a + b response.write a + b & "<br>" response.write a & b & "<br>" %> </body></html> Have to be the first line Declare a variable with a data type is not allowed: Dim x as Integer

  29. Response Object: Write Method <HTML><HEAD><TITLE>Hello World</TITLE> </HEAD> <BODY> <% response.write "Hello World" %> </BODY> <HTML><HEAD><TITLE>Message</TITLE></HEAD><BODY> <% Dim message message = "Hello World" Response.Write message %> </BODY></HTML> Hello World

  30. Math Operations <HTML><HEAD><TITLE>Math Operations</TITLE></HEAD><BODY> <% A = 7 B = 3 Response.Write A & " + " & B & " = " & A + B Response.Write "<BR>" Response.Write A & " - " & B & " = " & A - B Response.Write "<BR>" Response.Write A & " * " & B & " = " & A * B Response.Write "<BR>" Response.Write A & " / " & B & " = " & A / B %> </BODY></HTML> 7 + 3 = 107 - 3 = 47 * 3 = 217 / 3 = 2.33333333333333

  31. Decision Making and Branching • To vary the flow of a script, you use conditional statements (also known as controlstructures) to makes decisions during program execution. • The conditional statements include test expressions that are evaluated as the program runs and, based upon their results, control the program flow. • The two control structures that you will learn include • If…Then…Else • Select Case.

  32. Control Flow True Block False Block

  33. Dim MyDate 'Create a variable, MyDateMyDate = #2/13/95# 'Assign a date to MyDate' Test to see if MyDate is less than today's date' (which is returned by the Now function).' If it is, then MyDate is assigned today's dateIf MyDate < Now Then MyDate = Now • If value = 0 ThensForeColor = "Red"bBold = TruebItalic = TrueEnd If

  34. If…Then…Else and Nested If If value = 0 Then iForeColor = vbRed bBold = True bItalic = TrueElse iForecolor = vbBlack bBold = False bItalic = FalseEndIf If value = 0 Then iForeColor = vbRed bBold = TrueElseIf value=1 Then iForecolor = vbBlack bBold = FalseElseIf value=2 Then iForecolor = vbGreen bBold = FalseElse iForecolor = vbBlue bBold = FalseEnd If

  35. Nested If <HTML><HEAD><TITLE>Greeting</TITLE> </HEAD> <BODY> <% If Hour(Now) < 12 then Response.Write "Good Morning" Else If Hour(Now) < 18 then Response.Write "Good Afternoon" Else Response.Write "Good Night" End If End If %> </BODY></HTML>

  36. Making Decision Using Select Case • A Select Case structure works with a single test expression that is evaluated once at the top of the structure. • The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed. • Use the Case Else statement to handle any condition that did not match a specified case.

  37. Example Dim Color, MyVar Sub ChangeBackground (Color) MyVar = lcase (Color) Select Case MyVar Case "red" bgColor = "red" Case "green" bgColor = "green" Case "blue" bgColor = "blue" Case Else bgColor = "white" End Select End Sub

  38. Loops <HTML><HEAD><TITLE>Loops</TITLE></HEAD><BODY> <% Dim iCount For iCount = 1 to 5 Response.Write iCount Response.Write "<BR>" Next For iCount = 10 to 0 step -2 Response.Write iCount Response.Write "<BR>" Next %> </BODY></HTML> 123451086420

  39. Loops Statements • VBScript includes the following looping constructions: • Do...Loop: repeats statements while or until a test condition is met. • For...Next: repeats statements a specified number of times. • For Each element In group Next: repeats statements for each occurrence of a specific item in a specified group.

  40. Loop: Do While …… Loop <% Dim Counter Counter = 1 Do While Counter < 3 Response.Write "Counter = " & Counter & "<br>" Counter = Counter + 1 Loop %> Check condition at the beginning of the loop Counter = 1Counter = 2

  41. Do … Loop While • Dim Counter Counter = 1 Do Response.Write "Counter = " & Counter & "<br>" Counter = Counter + 1 Loop While Counter < 3 Check condition at the end of the loop

  42. Do Until • To check the test condition at the beginning of the loop, change the following statement: Do While Counter<3 …. Loop to the following: Do Until Counter>=3 … Loop • To check the condition at the end of the loop, change the following statement: Do … Loop While Counter<3 to the following: Do … Loop Until Counter>=3

  43. Square Number • Display the smallest positive integer that, when squared, is larger than 1037. <HTML><HEAD><TITLE>Square Number</TITLE> </HEAD><BODY> <% Dim iResult, iTest iTest = 1 Do Until iResult > 1037 iTest = iTest + 1 iResult = iTest * iTest Loop Response.Write iTest %> </BODY></HTML>

  44. Exercise: Form Input Data Processing <input type=text name=UserName>

  45. Array.asp <html> <% DIM x(3) x(0) = 1 x(1) = 2 x(2) = 3 x(3) = 4 %> <head><title>New Page 1</title></head><body> <% For k = 0 to Ubound(x) Response.Write( "x(" & k & ")=" & x(k) & "<br>") Next ' Total = x(0) + x(1) + x(2) + x(3) Total = 0 For k = 0 to Ubound(x) Total = Total + x(k) Next Response.Write( "The total is=" & Total) %> </body></html>

  46. Form Input Data Processing form4.htm <HTML><HEAD><TITLE>Form Input</TITLE></HEAD><BODY> <FORM METHOD=POST ACTION="answer04.asp"> Name: <INPUT TYPE=TEXT NAME="UserName"> <INPUT TYPE=SUBMIT> </FORM></BODY></HTML> answer04.asp <HTML><HEAD><TITLE>Form Input Processing</TITLE></HEAD><BODY> <% Response.Write Request.Form("UserName") %> </BODY></HTML>

  47. Form.htm <HTML><HEAD><TITLE>Order</TITLE></HEAD><BODY> <H2>Sample Order Form</H2> Please fill out this form, then click Submit:<P> <FORM METHOD="POST" ACTION="response.asp"> First Name: <INPUT NAME="fname" SIZE="48"><br> Last Name: <INPUT NAME="lname" SIZE="48"><br> Title: <INPUT NAME="title" TYPE=RADIO VALUE="mr">Mr. <INPUT NAME="title" TYPE=RADIO VALUE="ms">Ms. <P><INPUT TYPE=SUBMIT><INPUT TYPE=RESET> </FORM></BODY></HTML> http://127.0.0.1/vbscriptlab/form.htm

  48. Response.asp <%@ LANGUAGE = VBScript %> <HTML> <HEAD><TITLE>Response.asp File</TITLE></HEAD> <BODY BGCOLOR="#FFFFFF"><FONT FACE="ARIAL,HELVETICA"> <H2><CENTER>Order Received</CENTER></H2> <P ALIGN=CENTER> <%= Request.Form("fname") & " " & Request.Form("lname")%> </FONT> </BODY> </HTML>

  49. Response2.asp <%@ LANGUAGE = VBScript %> <HTML> <HEAD><TITLE>Response.asp File</TITLE></HEAD> <BODY BGCOLOR="#FFFFFF"><FONT FACE="ARIAL,HELVETICA"> <H2><CENTER>Order Received</CENTER></H2> <P ALIGN=CENTER> <% Title = Request.Form("title") %> <% LastName = Request.Form("lname") %> <%If Title = "mr" Then %> Mr. <%= LastName %> <% ElseIf Title = "ms" Then %> Ms. <%= LastName %> <% Else %> <%= Request.Form("fname") & " " & LastName %> <% End If %> </FONT> </BODY> </HTML> Change the action attribute of form.htm to response2.asp and save it as form2.htm

  50. Exercise: Calculator • Operator can be: +, -, *, / • Check divide-by-zero error Cal.htm A*B=600

More Related