Transcript Powerpoint

The Problem of State
We will look at…
 Sometimes web development is just plain weird!
 Internet / World Wide Web
 Aspects of their operation
 The role of clients and servers
 ASPX Page (Web Form)
 How it is passed between browser and server
 The structure of the page
 How it provides functionality to the browser
 The problem of state
The Internet and TCP/IP
 Network of networks
 Defence research in the 60s
 TCP/IP (Transmission Control Protocol / Internet
Protocol)
 Allows programs on computers to talk to each other
The IP Address
 Uniquely identifies each machine
 32 bit number made up of four 8 bit numbers
 Visit http://209.85.227.105/
 Assigned in blocks
www.dmu.ac.uk
www.cse.dmu.ac.uk
G677 (my server)
146.227.160.79
146.227.57.2
146.227.53.94
Name Servers
 http://209.85.227.105/ not obviously www.google.com
Ports




TCP/IP allows programs on machines to communicate
IP address identifies machine
port number identifies program
There is no law that states a specific port must be used for a
service however there are certain ports that traditionally
provide services.
 80 HTTP (web pages)
 21 FTP (File transfers)
 119 NNTP (Network News Transfer Protocol)
 443 HTTPS (secure web pages)
The Good Old Days
 Up until about 1989 the Internet existed quite happily
without the World Wide Web
 File Transfer Protocol (FTP)
 Telnet
 Usenet
 World Wide Web - Is not the internet!
The Web’s Client Server Model
Where is Client and Server in
Visual Studio?
Server v Client Side Code
 Code may be added at either end of the process
 Client side code runs at browser
 Action Script (Flash)
 JavaScript
 VBScript
 Server Side Code
 ASP.NET (C # VB.NET)
 PHP
 JSP
Server Side Code – Dynamic
Pages
HTML Forms GET and POST
 HTML allows simple form creation
HTML Form Code
Change POST to GET
http://g519-md.ad.cse.dmu.ac.uk/Request/?txtFirstName=Matthew&txtLastName=Dean&Submit1=submit
Active Server Pages (ASPX)
Events & Handlers
 User triggered events
Click
Triggered when a user presses a button
Selected Index Changed Activated when the user selects an item off a drop
down list
 System generated
Load
Runs when the ASPX page is loaded by the server
Unload
Runs when the ASPX page is unloaded from the
server
Create a Similar Form in ASP
Active Server Controls
Note the tag <asp
Post Back = False
 Post back is false on the first HTTP request
 The browser sends the request to the server for the page
 The server runs the page load event
 The server runs page unload event
 ASPX controls converted to HTML and sent to the
requesting browser
What the Browser Gets…
 ASP & Code never makes it to the browser!
Post Back = True
 The browser sends the HTTP request to the server
 The server runs the page load event
 The server runs other events (in this case the click
event of the Go button)
 The server runs the page unload event
 All asp controls changed into suitable HTML controls
and sent back to the requesting browser
 NOTE Load and Unload Events ALWAYS RUN!
The Problem of State
 We have seen the following points
 The web follows a client server mode of operation
 The ASPX page is rendered on the server and sent to the browser as HTML
 The page is rendered in two modes

PostBack = False
 (The first time the page is requested, Load – Unload events)

PostBack = True
 (Subsequent renderings of the page, Load – Other Events – Unload)
 The thing to note in all of this messing about is that the settings of the page are
not automatically remembered on each round trip.
 The web is referred to as stateless.
 So how is this problem addressed?
Use Cookies
 Cookies are small files stored on the client computer
that allow the web page to record details of its visit to
that machine.
 Cookies may be turned off by the user of the client
machine.
 Not suitable for sensitive data.
Use a Query String
 This is achieved by setting the HTML forms method to Get rather than
Post.
 This is a good technique so long as the data isn’t a potential security
risk.
 This would be a very bad query string.
 http://www.mysite.com/login.asp?UserName=Fred&Password=passwo
rd123
Use Session Variables
HTTP Request
Load session
variables in the
load event
Browser
Server
HTML Page
Save session
variables in the
unload event
 Use in conjunction with IsPostBack in the Load Event
of the page…
Potential Problem…
 Remember the load event runs every time the page is processed and it
is the first thing the server does.
 If we load the messages on subsequent renderings of the page we get
the following problem…
 I click an entry in the list and press delete
 The load event runs re-setting the list removing my selection
 The delete click event fails because the list has been re-set
 To avoid this kind of problem we need to check IsPostBack to see if it is
appropriate to read data at the server.
Summary
 Because the web is stateless and processes pages the
way that it does you will at some point get very
confused about state!
 Remember Load event runs first
 Other Events next
 Unload event last
 Load and Unload always run!