1 / 41

Server Side Scripting

Server Side Scripting. Introduction To ASP. HTML Page. Browser. Processes server script and converts it into HTML. Request for server Page. Database. Web Server. How Server Side Scripting Works. Active Server Pages.

acton
Download Presentation

Server Side Scripting

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. Server Side Scripting Introduction To ASP

  2. HTML Page Browser Processes server script and converts it into HTML Request for server Page Database Web Server How Server Side Scripting Works

  3. Active Server Pages • An Active Server Page (ASP) is an HTML page that includes one or more serverside scripts. • Used with Microsoft servers • PWS and Windows 98 • IIS and later versions of Windows • Has a .asp extension • Generally use VBScript but can use JScript • Has tags <% ASP code in here %>

  4. ASP (Cont) • Usually tailoring a page for theuser.   • Used in many applications • Dynamic individualisation of a page • Extraction of data from a database

  5. Where you can get ASP access • Using your ASP account at the university • W drive • View via http://internalasp.soc.staffs.ac.uk/student/ • Using many free ASP web areas • e.g. www.Brinkster.com • Most have adverts

  6. The Main Objects • Response • Session • Request • Server • Application

  7. Response Object • It is the object which communicates between the server and the output which is sent to the client. <body>   <% Response.Write("Hello, World!") %> </body> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex1.asp

  8. Response Object (Cont) • You can add HTML formatting in the response.write. <% response.write(“<p><font face=’Arial’ size=’4’><b>Hello World </b></font> </p>”)%> • You can invoke the Response.Write method by using the equals (=) sign. The = simulates Response.Write. <%="Hello, World!" %> • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex2.asp

  9. Simple Debugging • You can use the response.write as a debugging tool to show where your program gets to before it crashes, or to see what results you are getting.

  10. Redirecting using ASP • Sometimes, you may wish to redirect the user to a different URL automatically. • Response.Redirect “URL” • Must go before all HTML tags

  11. Loops and Conditionals

  12. For Loops • You can use FOR in ASP just as you can in standard programming <% FOR I=1 to 10 Response.write(“Hello world<br>”) NEXT %> • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex3.asp

  13. If Statement If … then ‘first bit code Else If … then ‘Second bit code End if End if http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex4.asp

  14. Case Statements • Used instead of multiple ifs • Select Case myVarA    Case "1"        Response.Write("a=1")    Case "2"        Response.Write("a=2")    Case "3"        Response.Write("a=3")    Case Else    Response.Write("a equals something else...")End Select • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex11.asp

  15. ASP Variables

  16. Variables • In ASP code, you do not have to declare variables. You just access them by saying VAR1 = 23 VAR2 = “Fred Bloggs” • Variable names • must start with alphabetical character • can't contain any full stops or reserved words • must be unique and be 255 characters or less • Variable naming should always stick to one style, like strLastName for strings

  17. Local Variables • Within an ASP script • Which can only be referenced when that page is executed. • VAR2 = “Fred Bloggs”

  18. Session Objects and Session Variables • A session is from when a browser is opened to when it is shut, or when a certain time has elapsed. • Session Variables are stored in memory on your server. • You can set session variables at any point. • Useful if you want to remember a value from one page to the next • E.g. When you log in to a website a session variable is set. This way you don't have to retype in your username and password for every page you go to.

  19. Session Variables • session(“VARIABLENAME”) = “Fred Bloggs” • Response.Write(Session("VarName")) • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspvar.asp • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex8.asp

  20. Session Object • Session.SessionID - the unique session identifier set automatically. • Session.TimeOut - amount of time in minutes that a session will stay alive. • To change - Session.TimeOut = NumberOfMinutes • This time, in minutes, is how long your session variables will be accessible. Unless the user closes their browser, then all session variables will be cleared. The default for the Session.TimeOut is 30 minutes.

  21. Adding variables together • You can add two numeric variables together using the + sign <% Age = 20 Pi = 3.14159 Response.write(Age + Pi) %> • You can do the same with two text variables • If you attempt to add a string to a number you get an error message • You can use the & rather than the + sign to treat all the variables as text • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex5.asp

  22. Checking the type of a variable • You may need to check the type of a variable. You can do this by using functions like • IsDate() • IsNumeric() • IsEmpty() – whether a variable has been initialised • IsNull()

  23. Example <% MyName = “Scott Mitchell” MyAge = 21 Response.Write(IsNumeric(MyAge)) Response.Write(“<br>”) Response.Write(IsDate(MyName)) If not IsDate(MyName) then response.write("Silly person...its not a date") end if %>http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex6.asp

  24. Converting Variables • You can make sure the variable is of a certain type by converting it using a function. • CBool Function - Converts to a Boolean • CByte Function - Converts to a Byte • CCur Function - Converts to a Currency • CDate Function - Converts to a Date • CDbl Function - Converts to a Double • CInt Function - Converts to a Integer • CSng Function - Converts to a Single • CStr Function - Converts to a String

  25. Converting Variables (Cont) • All of these functions take one parameter, a variable, and return a variable of the requested subtype. • To convert one variable to a particular subtype use the following syntax: var1 = CStr(var1)

  26. Formatting Variables • FormatCurrency(var1) • FormatPercent(var1) • Lcase(var1)/ Ucase(var1) • Left(var1, length) / Right(var1, length) • Mid(var1,start,length) • Len(var1) • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex7.asp

  27. Arrays • Arrays in ASP have a zero based index. • This means that the first item in the array is myArray(0). • The size of arrays in ASP can be changed while still maintaining the data that is currently in them.

  28. Arrays(Cont) • How to create • Dim myArray(2) / Dim myArray() • How to enlarge • ReDim Preserve myArray(3) • NOTE: If you are creating a small array you can also create it like this • FavoriteFoodArray = Array(“fried eggs",“ beef burgers",“chips") • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex9.asp

  29. Month(date1), Day(date1), Year(date1) Hour(time1), minute(time1) monthname(month(date1)) DateAdd(interval, number, date) DateDiff(interval, date1, date2) DatePart(interval, date1) FormatDateTime(Date) http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex12.asp “yyyy” - Year “q” - quarter “m” - Month “y” - Day of Year “d” - Day “w” - Weekday “ww” - Week of Year “h” - Hour “n” - Minute “s” - Second Date and Time Functions

  30. Functions and Subroutines

  31. Sub Routines and Functions • Useful if you are using the same bit of code more than once. • Sub Routines do not return a value whereas functions do. <% Function FunctionName(parameters passed in) 'Code for Function... ‘ Returns value End Function %> <% Sub SubroutineName( Parameters to Pass In ) 'Code for Sub... End sub %>

  32. Example ' This function takes two integers and adds them together. Function Add(intA, intB) intResult = intA + intB Add = intResult End Function %>……… <p>1 + 2 = <%= Add(1,2) %></p> <p>534 + 3142 = <%= Add(534,3142) %></p> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex10.asp

  33. ASP Using ASP with Forms

  34. Request Object • The Request Object is used to get information from an html form or on the query string • Forms • Post • Get • Using the URL with a ? • Fred.asp?var1=6

  35. From A Form POST In form1.htm <form method="POST" action="formresult.asp"> My Text Box <input type="text" name="myvar" size="20"> <input type="submit" value="Submit"> </form> In formresult.asp <% formvar = request.form("myvar") response.write(formvar) %> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex13.htm

  36. From A Form GET In form1.htm <form method=“GET" action="formresult.asp"> My Text Box <input type="text" name="myvar" size="20"> <input type="submit" value="Submit"> </form> In formresult.asp <% formvar = request.querystring("myvar") response.write(formvar) %> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex14.htm

  37. From A URL Works the same as a GET In formresult.asp?myvar=fred <% formvar = request.querystring("myvar") response.write(formvar) %> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex14b.asp?myvar=xfred

  38. From A Combination In form1.htm <form method="POST" action="formresult.asp?myvar2=6"> My Text Box <input type="text" name="myvar" size="20"> <input type="submit" value="Submit"> </form> In formresult.asp <% formvar = request.form("myvar") Formvar2 = request.querystring(“myvar2”) response.write(formvar) Response.write(formvar2) %> http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex15.htm

  39. Posting To The Original ASP Page • You may also in a form wish to have an action which posts to the same page. • Useful for validation • Useful if the response given by the user affects the page that the user is sent to (Using response.redirect) • http://asp.soc.staffs.ac.uk/student/flk1/examples/aspex16.asp

  40. Comments • Comments within ASP tags are done with a single ‘ <BODY><% Response.Write("Hello, World!") ‘This is a comment ‘This is another comment %> </BODY>

  41. Next Week • ASP and databases • Include Files • More loops • Sources Of ASP Code • Error Messages

More Related