1 / 22

CVEV 118/698 Active Server Pages

CVEV 118/698 Active Server Pages. Lecture 3. Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar. What is ASP?. ASP stands for Active Server Pages. It is a server side technology which is used to display dynamic content on web pages.

xander
Télécharger la présentation

CVEV 118/698 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. CVEV 118/698Active Server Pages Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar

  2. What is ASP? • ASP stands for Active Server 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. • ASPin 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.

  3. What Can You 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. • When you request a page with the extension .ASP: • theweb 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. • This ensures that an ASP page will work with any browser. • 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. • However, instead of starting and ending with lesser-than ( < ) and greater-than ( > ) brackets, they typically start with <% and end with %>. • The <% is called an opening tag, and 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.

  8. What It Looks Like • 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). <html><head><title>Mixture</title></head><body>The time currently is<%= Time %><br><font size="4" font color="red">and the date is</font><%= Date%> </body></html>

  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. <%‘This is a commentresponse.write “Hello” ‘This also is a comment%> • It is advisable that you clearly comment your code. • To print out code use <%=  or response.write. <%= “Hello” %> or<% response.write “Hello” %> • 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. • 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). • 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. • So better declare variables before using them: <%Dim IntVar%> • Moreover, if you turn on Option Explicit, you'll get an error any time you use a variable that has not been defined: <% Option Explicit %>

  12. String Manipulation • 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(). • 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. • 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 <% @ Language="VBScript" %><% Option Explicit %> <html> <head><title>Exam marks</title></head><body><%Dim Grade      'comment - variable declarationGrade=45       'variable holds the value 45%> <% If Grade>=70 Then response.write "You have a good grade"Else If Grade >=50 AND Grade <70 Then response.write "You have an average grade"Else response.write "You failed"End If %> </body> </html>

  14. Select Case • Select Case is quite similar to the If..Then..Else... <%SELECT CASE Number Case 1Response.write “Number is equal to 1” …Case ElseResponse.write "The number is not equal to any of the values above " END SELECT %> • Same applies to strings: <%SELECT CASE Name Case “String”… • Note: with SELECT CASE you cannot use < and >.

  15. Loops • Do While… Loop <% i = 0 Do While i < 10     response.write("Hello<HR>") i = i + 1 Loop %> • Do Until… Loop <% i = 0 Do Until i =10     response.write("Hello<BR>") i = i +1 Loop %> • For… Next Loop <% For i = 1 to 20 STEP 2 response.write("Hello<BR>") Next %>

  16. Arrays • 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: • ReDim Preserve strArray(3) ‘Contains 4 elements • 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): • 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. • 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. • Example:<%On Error Resume Nextresponse.write (“There is an error here!") If Err.number <> 0 then response.write Err.description End If %>

  18. Connecting to a Database • First instantiate the Connection Object: Set Connection = Server.CreateObject("ADODB.Connection") • 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. Connection.Open(sConnString) • 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 recordsetIf 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". <%myDSN=“DSN = DataSourceName"SQL=“INSERT INTO Friends (FirstName, LastName) “ & _ “VALUES (‘Walid', ‘Asmar’)”Set Connection = Server.CreateObject("ADODB.Connection")Connection.Open(myDSN)Connection.Execute(SQL)Connection.CloseSet Connection = Nothing%>

  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.MoveNext Loop End If … %>

  22. What’s Next ? • Nothing! 

More Related