420 likes | 743 Views
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.
E N D
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 • 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 %>
ASP (Cont) • Usually tailoring a page for theuser. • Used in many applications • Dynamic individualisation of a page • Extraction of data from a database
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
The Main Objects • Response • Session • Request • Server • Application
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
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
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.
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
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
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
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
ASP Variables
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
Local Variables • Within an ASP script • Which can only be referenced when that page is executed. • VAR2 = “Fred Bloggs”
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.
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
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.
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
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()
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
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
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)
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
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.
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
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
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 %>
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
ASP Using ASP with Forms
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
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
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
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
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
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
Comments • Comments within ASP tags are done with a single ‘ <BODY><% Response.Write("Hello, World!") ‘This is a comment ‘This is another comment %> </BODY>
Next Week • ASP and databases • Include Files • More loops • Sources Of ASP Code • Error Messages