mark dixon page 1 15 – web applications: server-side code (asp)
Post on 22-Dec-2015
216 views
TRANSCRIPT
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:
<html> <head> <title></title> <script language=vbscript> 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 </script> </head> <body> <p>Please logon: <input id=txtUserName type=text> <input id=txtPassWord type=text> <input id=btnLogon type=submit value=Logon> </body></html>
Logon.htm <html> <head> <title>My Home page</title> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body></html>
Home.htm
<html> <head> <title></title> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body></html>
LoginFail.htm
Mark Dixon Page 6
Problem: View Source• View Source – shows client-side script:
Reveals bothusername & password
Mark Dixon Page 7
networkconnection
Web Hardware and Software
ClientServer
BrowserApplication(MS Explorer,
Netscape)
Web-serverApplication
(MS IIS,Apache)
Mark Dixon Page 8
Request-Response Cycle: HTML
BrowserApplication(MS Explorer,
Netscape)
Web-serverApplication
(MS IIS,Apache)
http://mdixon.soc.plym.ac.uk/
Request
<html> <head> <title>Mark Dixon's web site</title> </head> <body background="BackGround.JPG"> <font size=+3><center><b> <p>Mark Dixon's web site</b></center> <font size=+2> <p>Welcome to my web server. Please select from the following list: <ul> <li><a href="./Soft131/Index.htm">Soft131: Introduction to programming for Multimedia and Internet applications.</a> </ul> </font> </body></html>
Response
Mark Dixon Page 9
Request-Response Cycle: CSC
BrowserApplication(MS Explorer,
Netscape)
Web-serverApplication
(MS IIS,Apache)
Logon.htm
Request
<html> <head> <title></title> <script language=vbscript> 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 </script> </head> <body> <p>Please logon: <input id=txtUserName type=text> <input id=txtPassWord type=text> <input id=btnLogon type=submit value=Logon> </body></html>
Response
Client-side code:Code sent to ClientInterpreted by browser
Mark Dixon Page 10
Request-Response Cycle: SSC
BrowserApplication(MS Explorer,
Netscape)
Web-serverApplication
(MS IIS,Apache)
http://localhost/datetime.asp
Request
<html> <head> <title>Today's date</title> </head> <body> <p>The date today is 21/11/2005<br> <p>The time is currently 10:28:18<br></body></html>
Response
<html> <head> <title>Today's date</title> </head> <body> <p>The date today is <% Response.Write(Format(Now, "D")) %></p> <p>The time is currently <% Response.Write(Format(Now, "T")) %></p> </body></html>
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 <% and %>
– Response object: page sent back to client
• write method: adds text to response object
– Now object:• current date (server)
<html> <head> <title>Today's date</title> </head> <body> <p>The date today is <% Response.Write(Format(Now, "D")) %></p> <p>The time is currently <% Response.Write(Format(Now, "T")) %></p> </body></html>
Date.aspx
Mark Dixon Page 13
Form Submission• action attribute
• submit button<html> <head> <title>Login</title> </head> <body> <p>Please login: <form name="frmLogin" action="Login.aspx" method=post> Username:<input name="txtUserName" type="text" /><br /> Password:<input name="txtPassWord" type="password" /><br /> <input name="btnLogin" type="submit" value="Login" /> </form> </body></html>
Login.htm
Mark Dixon Page 14
Form Processing
<html> <head> <title>Login</title> </head> <body> <% If Request.Form("txtUserName") = "George" Then Response.Write "Login successful." Else Response.Write("Invalid user name.") End If %> </body></html>
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
<html> <head> <title>Login</title> </head> <body> <% If Request.Form("txtUserName") = "George" Then Response.Write("Login successful.") Else Response.Write("Invalid user name.") End If %> </body></html>
LoginCheck.aspx
Server SW(IIS)
<html> <head> <title>Login</title> </head> <body> Invalid user name. </body></html>
Response
Mark Dixon Page 17
Example: Logon - code (v2)• Using Server-side VB Script:
<html> <head> <title></title> </head> <body> <p>Please logon: <form action=Login.aspx method=post> <input name=txtUserName type=text> <input name=txtPassWord type=text> <input name=btnLogon type=submit value=Logon> </form> </body></html>
Logon.htm
<html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body></html>
Login.aspx
<html> <head> <title>My Home page</title> </head> <body> <p>Welcome to my home page.<br> <img src="YouAreHere.jpg" WIDTH="450" HEIGHT="259"> </body></html>
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/RemoveWindows
Components
IIS
Mark Dixon Page 20
IIS: Enabling/Disabling• Start, Settings, Control Panel,
Administrative Tools, Internet Services Manager StopStart
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<html> <head> <title></title> <script language=vbscript> 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 </script> </head> <body> <p>Please logon: <input id=txtUserName type=text> <input id=txtPassWord type=text> <input id=btnLogon type=submit value=Logon> </body></html>
Logon.htm
<html> <head> <title></title> <% If Request.Form("txtUserName") = "mark" And Request.Form("txtPassWord") = "soft131" Then Response.Redirect("home.htm") End If %> </head> <body> <p>Sorry, those login details were incorrect. <p>Please try <a href=Logon.htm>again</a> </body></html>
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 (-32768 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 of e.g. 4 Integer/Long
Rooms
• Height e.g. 1.87m Single/Double
• Surname e.g. Smith String
• Car Reg e.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
<script language="vbscript">Dim xDim yDim sDim b x = 23 y = 18.5 s = "Hello there" b = false</script>
<script language="vbscript" runat="server">Dim x As LongDim y As DoubleDim s As StringDim b As Boolean x = 23 y = 18.5 s = "Hello there" b = false</script>
Mark Dixon Page 29
Example: Apples
<html> <head><title>Apples</title></head> <body> <p>How many apples do you want?</p> <form action="Apples.aspx" method="post"> <input name="txtApples" type="text" /> <input name="btnShow" type="submit" value="Show" /> </form> </body></html>
<html> <head><title>Apples</title></head> <body> <p>Here are your apples:</p> <% Dim s As String Dim i As Long Dim a As Long a = Request.Form("txtApples") s = "" For i = 1 To a s = s & "<img src='Apple.gif' />" Next Response.Write(s) %> </body></html>
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.