Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon Page 2 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in server-side code Objectives, by end of this week’s sessions, you should be able to: –create an asp web-page, including: HTML, and server-side VB script
Mark Dixon Page 3 Example: Logon page Logon pages – probably most common page –hotmail –Amazon –University portal –utility bills (gas, electricity, phone, internet) –Travel (flights, ferry, car rental)
Mark Dixon Page 4 Example: Logon Restrict access to home page
Mark Dixon Page 5 Example: Logon - code (v1) Using Client-side VB Script: Sub btnLogon_OnClick() If txtUserName.value = "mark" And txtPassWord.value = "soft131" Then window.navigate "Home.htm" Else window.navigate "LoginFail.htm" End If End Sub Please logon: Logon.htm My Home page Welcome to my home page. Home.htm Sorry, those login details were incorrect. Please try again LoginFail.htm
Mark Dixon Page 6 Problem: View Source View Source – shows client-side script: Reveals both username & password
Mark Dixon Page 7 network connection Web Hardware and Software Client Server Browser Application (MS Explorer, Netscape) Web-server Application (MS IIS, Apache)
Mark Dixon Page 8 Request-Response Cycle: HTML Browser Application (MS Explorer, Netscape) Web-server Application (MS IIS, Apache) Request Mark Dixon's web site Mark Dixon's web site Welcome to my web server. Please select from the following list: Soft131: Introduction to programming for Multimedia and Internet applications. Response
Mark Dixon Page 9 Request-Response Cycle: CSC Browser Application (MS Explorer, Netscape) Web-server Application (MS IIS, Apache) Logon.htm Request Sub btnLogon_OnClick() If txtUserName.value = "mark" And txtPassWord.value = "soft131" Then window.navigate "Home.htm" Else window.navigate "LoginFail.htm" End If End Sub Please logon: Response Client-side code: Code sent to Client Interpreted by browser
Mark Dixon Page 10 Request-Response Cycle: SSC Browser Application (MS Explorer, Netscape) Web-server Application (MS IIS, Apache) Request Today's date The date today is 21/11/2005 The time is currently 10:28:18 Response Today's date The date today is <% Response.Write(Format(Now, "D")) %> The time is currently <% Response.Write(Format(Now, "T")) %> Server-side code: Interpreted by server (code never sent to Client)
Mark Dixon Page 11 Server-side Script (what) ASP – active server pages –executed on server takes time – request-response cycle requires server software (e.g. IIS) –code not sent to client code secure (can't be viewed by client) –results (response) sent to client pages will NOT work by double clicking on file
Mark Dixon Page 12 Server-side Script (how) ASP code: –.aspx (not.htm) –between –Response object: page sent back to client write method: adds text to response object –Now object: current date (server) Today's date The date today is <% Response.Write(Format(Now, "D")) %> The time is currently <% Response.Write(Format(Now, "T")) %> Date.aspx
Mark Dixon Page 13 Form Submission action attribute submit button Login Please login: Username: Password: Login.htm
Mark Dixon Page 14 Form Processing Login <% If Request.Form("txtUserName") = "George" Then Response.Write "Login successful." Else Response.Write( "Invalid user name.") End If %> Login.aspx
Mark Dixon Page 15 View Source Code executed at server –code is never sent to client View, Source – does not show code:
Mark Dixon Page 16 Code Execution Login <% If Request.Form("txtUserName") = "George" Then Response.Write("Login successful.") Else Response.Write("Invalid user name.") End If %> LoginCheck.aspx Server SW (IIS) Login Invalid user name. Response
Mark Dixon Page 17 Example: Logon - code (v2) Using Server-side VB Script: Please logon: Logon.htm <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> Sorry, those login details were incorrect. Please try again Login.aspx My Home page Welcome to my home page. Home.htm
Mark Dixon Page 18 Running your ASP page within Visual Studio –Run (play) button (F5) –only available to you on development PC using Internet Information Services (IIS) –makes PC a server –page available to all computers on internet
Mark Dixon Page 19 IIS - Installing IIS / personal web server on Windows CD Start, Settings, Control Panel, Add/Remove Programs Add/Remove Windows Components IIS
Mark Dixon Page 20 IIS: Enabling/Disabling Start, Settings, Control Panel, Administrative Tools, Internet Services Manager Stop Start
Mark Dixon Page 21 IIS: Exposing pages Put ASP pages in: –C:\INetPub\wwwRoot (this part of hard disk exposed to outside world) Execute pages by putting: –localhost (in web browser, e.g. IE, means local machine) ASP pages don't work by double-clicking
Mark Dixon Page 22 IIS – Date.asp localhost/test/date.aspx C:\INetPub\wwwRoot\Date.aspx
Mark Dixon Page 23 Reference: Server Object Model Request object: calling web page –Form: used to get form data from previous page Response object: web page sent back –Write: used to put text into web page –Redirect: used to navigate to other page –Clear: erases all HTML in web page
Mark Dixon Page 24 Client-side vs. Server-side Code Sub btnLogon_OnClick() If txtUserName.value = "mark" And txtPassWord.value = "soft131" Then window.navigate "Home.htm" Else window.navigate "LoginFail.htm" End If End Sub Please logon: Logon.htm <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> Sorry, those login details were incorrect. Please try again LoginFail.aspx Both use VB Script language (i.e. Sub, If, Dim, For, etc.) Objects are different
Mark Dixon Page 25 Data Types Variant – all types of data –slow, memory hungry Boolean – true or false Integer – whole numbers ( to 32768) Long – whole numbers (large) Single – decimal numbers Double – decimal numbers (more precise) String – text Object – object instances
Mark Dixon Page 26 Data Type Selection Number ofe.g. 4 Integer/Long Rooms Heighte.g. 1.87m Single/Double Surnamee.g. Smith String Car Rege.g. XY55 ABC String
Mark Dixon Page 27 Using data types Variable declaration Dim x As Long Parameters Sub Thing(boo As String, y As Long) Functions Function IsTall() As Boolean
Mark Dixon Page 28 client-side vs. server-side code client-side code – only variant server-side code – all data types Dim x Dim y Dim s Dim b x = 23 y = 18.5 s = "Hello there" b = false Dim x As Long Dim y As Double Dim s As String Dim b As Boolean x = 23 y = 18.5 s = "Hello there" b = false
Mark Dixon Page 29 Example: Apples Apples How many apples do you want? Apples Here are your apples: <% Dim s As String Dim i As Long Dim a As Long a = Request.Form("txtApples") s = "" For i = 1 To a s = s & " " Next Response.Write(s) %> Apples.htm Apples.aspx
Mark Dixon Page 30 Tutorial Exercise: Login LEARNING OBJECTIVE: create an ASP page, including HTML and server-side VB Script Task 1: Get the Login (v2) example from the lecture working. Task 2: Use view source – you should not be able to see the code.
Mark Dixon Page 31 Tutorial Exercise: Date LEARNING OBJECTIVE: create an ASP page, including HTML and server-side VB Script Task 1: Get the Date example from the lecture working. Task 2: Add code that displays good morning/afternoon/evening/night, depending on the time of day.
Mark Dixon Page 32 Tutorial Exercise: Apples LEARNING OBJECTIVE: use variables with specific data types in ASP code Task 1: Get the apples example (from the lecture) working. Task 2: Modify your program so that the user enters another number, and the code adds a new line tag for that number of apples. Hint: Within the loop divide the number of apples by the second number, if the result is a whole number add a new line tag.
Mark Dixon Page 33 Tutorial Exercise: Student Loan LEARNING OBJECTIVE: create an ASP page, including HTML and server-side VB Script from scratch to solve a problem Task 1: Create a web site that allows the user to enter their salary and the computer calculates the annual and monthly payments for their student loan. Hint: You will need 2 pages (1 HTML with a form, linked to another ASPX file with code to do the calculation and display the result) Go to the student loans web-site to get the rules for repayment.