Download presentation
Presentation is loading. Please wait.
1
CVEV 118/698 Active Server Pages Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar
2
What is ASP? ASP stands for Active Server Pages. ASP stands for Active Server Pages. It is a server side technology which is used to display dynamic content on web pages. It is a server side technology which is used to display dynamic content on web pages. For example you could write code that would give you different information depending on what browser version you are using. For example you could write code that would give you different information depending on what browser version you are using. ASP in itself isn't a language it is more a technology developed by Microsoft. ASP in itself isn't a language it is more a technology developed by Microsoft. ASP can be used with various scripting languages such as Vbscript, Perl or Jscript. ASP can be used with various scripting languages such as Vbscript, Perl or Jscript.
3
What Can You Do with ASP? There are many things you can do with ASP: There are many things you can do with ASP: – You can easily access databases and present the results on your web site. – You can easily update, edit or delete content on a page. – You can create login and password protected sites. – You can create customized pages that display only things that will be of interest to a particular user, display different pages for different browsers, etc. – You can have pictures that display randomly. – You can display the date, time and other information in various different formats. – You can make a survey form and ask people who visit your site to fill it out, send emails, save the information to a file, etc.
4
Server-Side Technology ASP is run server-side. ASP is run server-side. When you request a page with the extension.ASP: When you request a page with the extension.ASP: – the web server looks and locates the page – then it executes the ASP script contained in the page and – sends back to your browser a pure html page. The server can be a computer on the internet, or on a local intranet. The server can be a computer on the internet, or on a local intranet. This ensures that an ASP page will work with any browser. This ensures that an ASP page will work with any browser. This is what is meant when by server side technology: This is what is meant when by server side technology: All the ASP code is processed first of all on the server before being sent back to be displayed by your browser.
5
HTML Page Display
6
ASP Page Display Note: ASP pages end with the file extension.asp as opposed to html files that have the.html extension.
7
Server-Side Scripts Server-side scripts look a lot like HTML tags. Server-side scripts look a lot like HTML tags. However, instead of starting and ending with lesser-than ( ) brackets, they typically start with. However, instead of starting and ending with lesser-than ( ) brackets, they typically start with. The is called a closing tag. In between these tags are the Server-Side Scripts. The is called a closing tag. In between these tags are the Server-Side Scripts. You can insert server-side scripts anywhere in your Web page - even inside HTML tags. You can insert server-side scripts anywhere in your Web page - even inside HTML tags.
8
What It Looks Like Your ASP page will invariably be a mixture of text, ASP, and html tags. Your ASP page will invariably be a mixture of text, ASP, and html tags. Example: In yellow is the html. In red is the text and in white is the ASP (Vbscript). Example: In yellow is the html. In red is the text and in white is the ASP (Vbscript). Mixture The time currently is and the date is Mixture The time currently is and the date is
9
Comments Just to point out that using an apostrophe at the start of a line or anywhere in a line of ASP code comments out that code. It will not be processed. Just to point out that using an apostrophe at the start of a line or anywhere in a line of ASP code comments out that code. It will not be processed. It is advisable that you clearly comment your code. It is advisable that you clearly comment your code. To print out code use <%= or response.write. To print out code use <%= or response.write. or or Note: delimiters don’t have to be on the same line of code. Note: delimiters don’t have to be on the same line of code.
10
ASP Primary Language VBScript is the default language for ASP scripts. VBScript is the default language for ASP scripts. If you are using something other than VBScript, you must specify the language; at the top of the page, add this line: If you are using something other than VBScript, you must specify the language; at the top of the page, add this line:<%@LANGUAGE=ScriptingLanguage%>
11
VBScript & Variables In VBScript, you don't have to declare variables or explicitly define their type (they are variant by default). In VBScript, you don't have to declare variables or explicitly define their type (they are variant by default). A variable exists the first time you use it. A variable exists the first time you use it. However, if you mistype a variable name somewhere in the code, a new variable is created. Your script may not work properly, and you may not even realize it. However, if you mistype a variable name somewhere in the code, a new variable is created. Your script may not work properly, and you may not even realize it. So better declare variables before using them: So better declare variables before using them: Moreover, if you turn on Option Explicit, you'll get an error any time you use a variable that has not been defined: Moreover, if you turn on Option Explicit, you'll get an error any time you use a variable that has not been defined:
12
String Manipulation InStr(StrBeingSearched, StrSearchingFor) returns the position at which the string being search for was located. InStr(StrBeingSearched, StrSearchingFor) returns the position at which the string being search for was located. Left(StrToPullFrom, NumofCharactersToPull) returns the numbers of characters you state starting from the left. Same for Right(). Left(StrToPullFrom, NumofCharactersToPull) returns the numbers of characters you state starting from the left. Same for Right(). Len(StrToCheck) returns the length of the string. Len(StrToCheck) returns the length of the string. LCase() and UCase()to pass in the string variable and convert the string into either uppercase or lowercase. LCase() and UCase()to pass in the string variable and convert the string into either uppercase or lowercase. Trim(strName) returns strName without white spaces on its left or right side. To trim the right/left side, use Rtrim (strName) or Ltrim(strName). Trim(strName) returns strName without white spaces on its left or right side. To trim the right/left side, use Rtrim (strName) or Ltrim(strName).
13
If… Then… Else… End If Exam marks Exam marks =70 Then response.write "You have a good grade" Else If Grade >=50 AND Grade =70 Then response.write "You have a good grade" Else If Grade >=50 AND Grade
14
Select Case Select Case is quite similar to the If..Then..Else... Select Case is quite similar to the If..Then..Else... <% SELECT CASE Number Case 1 Response.write “ Number is equal to 1 ” … Case Else Response.write "The number is not equal to any of the values above " END SELECT %> Same applies to strings: Same applies to strings: <% SELECT CASE Name Case “String” … Note: with SELECT CASE you cannot use. Note: with SELECT CASE you cannot use.
15
Loops Do While… Loop Do While… Loop <%i = 0 Do While i < 10 <%i = 0 Do While i < 10 response.write("Hello ") i = i + 1 Loop %> Do Until… Loop Do Until… Loop <% i = 0 Do Until i =10 <% i = 0 Do Until i =10 response.write("Hello ") i = i +1 Loop %> For… Next Loop For… Next Loop ") Next %> ") Next %>
16
Arrays Create an array (arrays in ASP have a zero based index): Create an array (arrays in ASP have a zero based index): – Dim strArray(2) ‘strArray contains 3 elements – Dim strArray = Array (“a”,”b”,”c”) Enlarge an array: Enlarge an array: – ReDim Preserve strArray(3) ‘Contains 4 elements Split an array: Split an array: Split(StrToSplit[, DelimiterToUse, HowMany]) – Returns a one dimensional array – DelimiterToUse and HowMany are optional parameters. – If you leave out the DelimterToUse, the function uses a space to split the string. Multi-Dimensional arrays, ArrayName (col, row): Multi-Dimensional arrays, ArrayName (col, row): – Dim strArray(2,3) ‘strArray contains 3 * 4 elements
17
Error Handling A good way to deal with an error is to use On Error Resume Next. A good way to deal with an error is to use On Error Resume Next. This tells the server to resume the next line of code if it encounters an error. This tells the server to resume the next line of code if it encounters an error. On Error Resume Next is usually placed at the top of each page. On Error Resume Next is usually placed at the top of each page. Example: 0 then response.write Err.description End If %> Example: 0 then response.write Err.description End If %>
18
Connecting to a Database First instantiate the Connection Object: First instantiate the Connection Object: Set Connection = Server.CreateObject("ADODB.Connection") Next define the actual connection string: Next define the actual connection string: sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _ "DBQ=" & Server.MapPath("databasename.mdb") & ";" Note: This statement tells the ADO (Active Data Objects) that we want to use the MS Access driver where our database is physically located. The databasename.mdb will be in the same folder as your asp page. Open the connection to the database. Open the connection to the database.Connection.Open(sConnString) Execute a SQL statement: Execute a SQL statement: sql = "SELECT * FROM myTable" Set Recordset = Connection.Execute(sql)
19
Example <%‘declare your variables Dim Connection, sConnString,sql,recordset Set Connection = Server.CreateObject("ADODB.Connection") sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _ "DBQ=" & Server.MapPath("databasename.mdb") & ";" Connection.Open(sConnString) sql = "SELECT * FROM myTable" Set Recordset = Connection.Execute(sql) ‘Check if there are any records in the recordset If not Recordset.eof Then response.write "There are records" Else response.write "There are no records" End if Recordset.Close Set Recordset = Nothing Connection.Close Set Connection = Nothing%>
20
Inserting Data in a DB Table The code below inserts data into the table "Friends". The code below inserts data into the table "Friends".
21
Displaying Records from a Table <%… If Rst.EOF and Rst.BOF Then Response.Write("No records returned.") Else Do While Not Rst.EOF Response.Write(Rst("Firstname")&" - "&Rst("Lastname")) Rst.MoveNextLoop End If …%>
22
What’s Next ? Nothing! Nothing!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.