Server Side Scripting - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Server Side Scripting

Description:

You can make sure the variable is of a certain type by converting it using a function. ... FavoriteFoodArray = Array('fried eggs',' beef burgers','chips' ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 42
Provided by: Fi5
Category:
Tags: scripting | server | side

less

Transcript and Presenter's Notes

Title: Server Side Scripting


1
Server Side Scripting
  • Introduction To ASP

2
How Server Side Scripting Works
3
Active Server Pages
  • An Active Server Page (ASP) is an HTML page that
    includes one or more server side 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 lt ASP code in here gt

4
ASP (Cont)
  • Usually tailoring a page for the user.  
  • 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/stude
    nt/
  • 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.
  • ltbodygt   lt Response.Write("Hello, World!") gt
  • lt/bodygt
  • http//asp.soc.staffs.ac.uk/student/flk1/examples/
    aspex1.asp

8
Response Object (Cont)
  • You can add HTML formatting in the
    response.write.
  • lt response.write(ltpgtltfont faceArial
    size4gtltbgtHello World lt/bgtlt/fontgt lt/pgt)gt
  • You can invoke the Response.Write method by using
    the equals () sign. The simulates
    Response.Write.
  • lt"Hello, World!" gt
  • 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
  • lt FOR I1 to 10
  • Response.write(Hello worldltbrgt)
  • NEXT gt
  • 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.W
    rite("a1")    Case "2"        Response.Write("a
    2")    Case "3"        Response.Write("a3")  
      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
  • lt Age 20
  • Pi 3.14159
  • Response.write(Age Pi) gt
  • 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
  • lt
  • MyName Scott Mitchell
  • MyAge 21
  • Response.Write(IsNumeric(MyAge))
  • Response.Write(ltbrgt)
  • Response.Write(IsDate(MyName))
  • If not IsDate(MyName) then
  • response.write("Silly person...its not a
    date")
  • end if
  • gt http//asp.soc.staffs.ac.uk/student/flk1/exam
    ples/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
Date and Time Functions
  • yyyy - Year
  • q - quarter
  • m - Month
  • y - Day of Year
  • d - Day
  • w - Weekday
  • ww - Week of Year
  • h - Hour
  • n - Minute
  • s - Second
  • 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

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.
  • lt Function FunctionName(parameters passed in)
  • 'Code for Function...
  • Returns value
  • End Function gt
  • lt Sub SubroutineName( Parameters to Pass In )
  • 'Code for Sub...
  • End sub gt

32
Example
  • ' This function takes two integers and adds them
    together.
  • Function Add(intA, intB)
  • intResult intA intB
  • Add intResult
  • End Function
  • gt
  • ltpgt1 2 lt Add(1,2) gtlt/pgt
  • ltpgt534 3142 lt Add(534,3142) gtlt/pgt
  • 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?var16

35
From A Form POST
  • In form1.htm
  • ltform method"POST" action"formresult.asp"gt
  • My Text Box ltinput type"text" name"myvar"
    size"20"gt
  • ltinput type"submit" value"Submit"gt
  • lt/formgt
  • In formresult.asp
  • lt
  • formvar request.form("myvar")
  • response.write(formvar)
  • gt http//asp.soc.staffs.ac.uk/student/flk1/examp
    les/aspex13.htm

36
From A Form GET
  • In form1.htm
  • ltform methodGET" action"formresult.asp"gt
  • My Text Box ltinput type"text" name"myvar"
    size"20"gt
  • ltinput type"submit" value"Submit"gt
  • lt/formgt
  • In formresult.asp
  • lt
  • formvar request.querystring("myvar")
  • response.write(formvar)
  • gt http//asp.soc.staffs.ac.uk/student/flk1/examp
    les/aspex14.htm

37
From A URL
  • Works the same as a GET
  • In formresult.asp?myvarfred
  • lt
  • formvar request.querystring("myvar")
  • response.write(formvar)
  • gt
  • http//asp.soc.staffs.ac.uk/student/flk1/examples/
    aspex14b.asp?myvarxfred

38
From A Combination
  • In form1.htm
  • ltform method"POST" action"formresult.asp?myvar2
    6"gt
  • My Text Box ltinput type"text" name"myvar"
    size"20"gt
  • ltinput type"submit" value"Submit"gt
  • lt/formgt
  • In formresult.asp
  • lt
  • formvar request.form("myvar")
  • Formvar2 request.querystring(myvar2)
  • response.write(formvar)
  • Response.write(formvar2)
  • gt http//asp.soc.staffs.ac.uk/student/flk1/exampl
    es/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
  • ltBODYgtlt
  • Response.Write("Hello, World!")
  • This is a comment
  • This is another comment
  • gt
  • lt/BODYgt

41
Next Week
  • ASP and databases
  • Include Files
  • More loops
  • Sources Of ASP Code
  • Error Messages
Write a Comment
User Comments (0)
About PowerShow.com