420 likes | 535 Views
Active Server Pages. Static Internet. In the early days of the Internet, all information sent to client’s browser was static. Little interactivity. Little functionality beyond simple hyperlinking. Dynamic Internet, Part 1 (CGI).
E N D
Static Internet • In the early days of the Internet, all information sent to client’s browser was static. • Little interactivity. • Little functionality beyond simple hyperlinking.
Dynamic Internet, Part 1 (CGI) • Common Gateway Interface (CGI) permits web browsers to request execution of application on the web server. • The output of application is HTML code, which is sent to requesting browser. • Separate CGI application loaded for each request. • Development of client-side scripting languages; especially JavaScript and VBScript.
Dynamic Internet, Part 2 (ISAPI) • An alternative to CGI introduced by Microsoft. • Uses dynamic link libraries (DLL). Once loaded, DLL stays in memory and answers user requests until explicitly released from memory. • Normally faster than an equivalent CGI application.
ISAPI filter • Custom DLL in same memory space as the web server, called by web server in response to every HTTP request. • The filter tells the web server how to handle the request. • The ASAPI filter allows you to customize the web server’s response to specific types of user requests.
Examples of ASAPI filters • Security layer between client and web server. Screens client request. • Custom filter to map client’s request to different location on the server. • Custom filter to interpret information from server, and present the information in a different format. For example, ASP.DLL, which interprets server code and gives the client customized information
Active Server Pages • In ASP.DLL. • Whenever user requests a file with .asp extension, this ASAPI filter handles the interpretation. • Loads scripting language interpreter DLLs. • Executes server-side code. • Passes resulting HTML to the web server, which forwards it to the browser.
Simple example <HTML> <BODY> Hello.<BR> It is now approximately <% =Time() %> at the server. <P> <% For i = 1 to 5 %> <FONT SIZE = <% =i %> > Hello Size <% =i %> </FONT> <BR> <% Next %> </BODY> </HTML>
ASP Object Model • ASP encapsulates the properties and methods of 6 objects: • Application • ASPError • Request • Response • Server • Session
Application Object • Represents the ASP application itself • One Application object for all users. • Two methods • Application_OnStart (triggered by first user) • Application_OnEnd (triggered by administrator explicitly closing application.
ASPError Object • To access properties of last error in currently executing script. • Accessible only through Server’s GetLastError method.
Request Object • Access HTML form-based data and parameters sent over the address line. • Receive HTTP cookie information. • Receive client certificate information • Get access to information in HTTP request header (via ServerVariables collection).
Response Object • Controls how data is sent to client. • Sends cookies to client, set expiration date. • Redirects user to another URL.
Server Object • Gives access to web server itself. • Set timeout variables for scripts • CreateObject method lets you create instances of server-side components. For example, ActiveX Data Objects to handle database access.
Session Object • Holds information specific to user’s current session on the web server.
Server-Side Scripting • ASP.DLL interprets code in .asp files that’s delimited with <% %>. • VBScript is the default scripting language. • If the first statement is: <% @LANGUAGE=Jscript %> you can change the scripting language.
Simple example <HTML> <BODY> Hello.<BR> It is now approximately <% =Time() %> at the server. <P> <% For i = 1 to 5 %> <FONT SIZE = <% =i %> > Hello Size <% =i %> </FONT> <BR> <% Next %> </BODY> </HTML>
An example that uses Form Data <html> <body> <form method="POST" action="http://localhost/asp/ex2.asp"> <H2>Choose your options</H2> <select name="favoriteBird"> <option value="flamingo"> Flamingo <option value="robin"> Robin <option value="cardinal"> Cardinal <option value="eagle"> Eagle <option value="penguin"> Penguin <option value="bluejay"> Blue Jay </select> <BR> User Name: <input type=text name="userName"> <BR> Color: <input type=text name="favoriteColor"> <BR> <input type=submit value="Send Data"> </form> </body> </html>
The ASP program <HTML> <HEAD> <BODY> <% username = Request("userName") %> <% color = Request("favoriteColor") %> <% bird = Request("favoriteBird") %> <FONT COLOR='<% Response.Write color %>' > Hello, <% Response.Write userName %>. </FONT> <BR> Your favorite bird is the <% Response.Write bird %>. Here is a picture! <P> <IMG SRC=<% Response.Write bird %>.jpg> </BODY> </HTML>
The generated code <HTML> <HEAD> <BODY> <FONT COLOR='blue' > Hello, John Avitabile. </FONT> <BR> Your favorite bird is the eagle. Here is a picture! <P> <IMG SRC=eagle.jpg> </BODY> </HTML>
An example using file access <html> <body> <form method="POST" action="http://localhost/asp/ex3.asp"> <H2>Choose your favorite bird</H2> <select name="favoriteBird"> <option value="flamingo"> Flamingo <option value="robin"> Robin <option value="cardinal"> Cardinal <option value="eagle"> Eagle <option value="penguin"> Penguin <option value="bluejay"> Blue Jay </select> <P> <input type=submit value="Send Vote"> </form> </body> </html>
The ASP Program (part 1) <% Set fso = CreateObject("Scripting.FileSystemObject") ' ActiveX voteFile = "C:\Inetpub\wwwroot\asp\votes.txt" Set tso = fso.OpenTextFile(voteFile, 8, True) ' TextStreamObject ' 8 is append, 1 is read, 2 is write; True means create if nec. Call tso.WriteLine(Request("favoriteBird")) Call tso.Close() Set tso = fso.OpenTextFile(voteFile, 1, True) Dim vote(6) Do line = tso.ReadLine Select Case line Case "flamingo" vote(1) = vote(1) + 1 Case "robin" vote(2) = vote(2) + 1 Case "cardinal" vote(3) = vote(3) + 1 Case "eagle" vote(4) = vote(4) + 1 Case "penguin" vote(5) = vote(5) + 1 Case "bluejay" vote(6) = vote(6) + 1 End Select Loop While Not tso.AtEndOfStream
The ASP Program (part 2) Call tso.Close() %> <HTML> <HEAD> <BODY> <% bird = Request("favoriteBird") %> Your vote for <% Response.Write bird %> has been recorded. Here are the totals so far. <TABLE BORDER=2 WIDTH=50%> <TR> <TD> Flamingo </TD> <TD> <% Response.Write vote(1) %> </TD> </TR> <TR> <TD> Robin </TD> <TD> <% Response.Write vote(2) %> </TD> </TR> <TR> <TD> Cardinal </TD> <TD> <% Response.Write vote(3) %> </TD> </TR> <TR> <TD> Eagle </TD> <TD> <% Response.Write vote(4) %> </TD> </TR> <TR> <TD> Penguin </TD> <TD> <% Response.Write vote(5) %> </TD> </TR> <TR> <TD> Blue Jay </TD> <TD> <% Response.Write vote(6) %> </TD> </TR> </TABLE> </BODY> </HTML>
The generated code <HTML> <HEAD> <BODY> Your vote for eagle has been recorded. Here are the totals so far. <TABLE BORDER=2 WIDTH=50%> <TR> <TD> Flamingo </TD> <TD> 2 </TD> </TR> <TR> <TD> Robin </TD> <TD> 3 </TD> </TR> <TR> <TD> Cardinal </TD> <TD> 2 </TD> </TR> <TR> <TD> Eagle </TD> <TD> 2 </TD> </TR> <TR> <TD> Penguin </TD> <TD> 1 </TD> </TR> <TR> <TD> Blue Jay </TD> <TD> 2 </TD> </TR> </TABLE> </BODY> </HTML>
Three Tier Architecture • User Interface created using HTML, Dynamic HTML, or XML. Possibly including client-side scripts. • Web server as middle tier that manipulates data from database via SQL queries and communicates with client Web browser. • Database.
Accessing a database <html> <body> <form method="POST" action="http://localhost/asp/ex4.asp"> <P> <input type=submit value="See Vote Totals"> </form> </body> </html>
The ASP Program (part 1) <% ' Check for existing connection to Database If IsObject( Session("birdVotes_dbConn")) Then Set dbConn = Session("birdVotes_dbConn") Else Set dbConn = Server.CreateObject( "ADODB.Connection" ) Call dbConn.Open("birdVotes","","") ' ODBC registered Set Session("birdVotes_dbConn") = dbConn End If dbQuery = "SELECT * FROM Votes" ' Create recordset Set votesRS = Server.CreateObject( "ADODB.Recordset") Call votesRS.Open( dbQuery, dbConn) ' move to first record in recordset Call votesRS.MoveFirst() %>
The ASP Program (part 2) <HTML> <HEAD> <BODY> <h1> Votes Already Cast </h1> <TABLE BORDER=2 WIDTH=50%> <TR> <TH> Voter </TH> <TH> Bird </TH> </TR> <% While Not votesRS.EOF %> <TR> <TD> <% =votesRS("Voter") %> </TD> <TD> <% =votesRS("Bird") %> </TD> </TR> <% Call votesRS.MoveNext() Wend %> </TABLE> </BODY> </HTML>
The generated code <HTML> <HEAD> <BODY> <h1> Votes Already Cast </h1> <TABLE BORDER=2 WIDTH=50%> <TR> <TH> Voter </TH> <TH> Bird </TH> </TR> <TR> <TD> Babe Ruth </TD> <TD> flamingo </TD> </TR> <TR> <TD> Lou Gehrig </TD> <TD> penguin </TD> </TR> <TR> <TD> Joe DiMaggio </TD> <TD> cardinal </TD> </TR> <TR> <TD> Mickey Mantle </TD> <TD> bluejay </TD> </TR> <TR> <TD> Reggie Jackson </TD> <TD> eagle </TD> </TR> <TR> <TD> Don Mattingly </TD> <TD> flamingo </TD> </TR> <TR> <TD> Mariano Rivera </TD> <TD> penguin </TD> </TR> <TR> <TD> Derek Jeter </TD> <TD> robin </TD> </TR> <TR> <TD> Ron Guidry </TD> <TD> robin </TD> </TR> <TR> <TD> Yogi Berra </TD> <TD> robin </TD> </TR> <TR> <TD> Whitey Ford </TD> <TD> eagle </TD> </TR> </TABLE> </BODY> </HTML>
Inserting, Updating, Complex queries <html> <body> <form method="POST" action="http://localhost/asp/ex5.asp"> <H2>Choose your favorite bird</H2> <select name="favoriteBird"> <option value="flamingo"> Flamingo <option value="robin"> Robin <option value="cardinal"> Cardinal <option value="eagle"> Eagle <option value="penguin"> Penguin <option value="bluejay"> Blue Jay </select> <P> Name: <input type=text name=userName> <P> <input type=submit value="Send Vote"> </form> </body> </html>
ASP Program (part 1) <% ' Check for existing connection to Database If IsObject( Session("birdVotes_dbConn")) Then Set dbConn = Session("birdVotes_dbConn") Else Set dbConn = Server.CreateObject( "ADODB.Connection" ) Call dbConn.Open("birdVotes","","") ' ODBC registered Set Session("birdVotes_dbConn") = dbConn End If dbQuery = "SELECT * FROM Votes" ' Create recordset Set votesRS = Server.CreateObject( "ADODB.Recordset") Call votesRS.Open( dbQuery, dbConn) ' move to first record in recordset
ASP Program (part 2) Call votesRS.MoveFirst() previousVote = False message = Request("userName") & "," While Not votesRS.EOF If Request("userName") = votesRS("Voter") Then message = message + " you changed your vote from " + _ votesRS("Bird") + " to " + Request("favoriteBird") sqlString = "UPDATE Votes SET Bird = '" + Request("favoriteBird") _ + "' WHERE Voter = '" + Request("userName") + "'" dbConn.Execute(sqlString) previousVote = True End If Call votesRS.MoveNext() Wend
ASP Program (part 3) If previousVote = False Then message = message + " your vote for " + Request("favoriteBird") + _ " has been recorded" sqlString = "INSERT INTO Votes VALUES ('" + Request("userName") + _ "', '" + Request("favoriteBird") + "')" dbConn.Execute(sqlString) End If %> <HTML> <HEAD> <BODY> <% =message %> <TABLE> <TR> <TH> Bird </TH> <TH> Votes </TH> </TR>
ASP Program (part 4) <% dbQuery = "SELECT Votes.Bird, COUNT(*) AS BirdCount FROM Birds " _ "INNER JOIN Votes " + _ "ON Birds.Bird = Votes.Bird GROUP BY Votes.Bird" Set birdsRS = Server.CreateObject( "ADODB.Recordset") Call birdsRS.Open( dbQuery, dbConn) ' move to first record in recordset Call birdsRS.MoveFirst() While Not birdsRS.EOF %> <TR> <TD> <% =birdsRS("Bird") %> </TD> <TD> <% =birdsRS("BirdCount") %> </TD> </TR> <% Call birdsRS.MoveNext() Wend %> </TABLE> </BODY> </HTML>
Code generated <HTML> <HEAD> <BODY> Thurman Munson, you changed your vote from robin to penguin <TABLE> <TR> <TH> Bird </TH> <TH> Votes </TH> </TR> <TR> <TD> bluejay </TD> <TD> 2 </TD> </TR> <TR> <TD> cardinal </TD> <TD> 1 </TD> </TR> <TR> <TD> eagle </TD> <TD> 2 </TD> </TR> <TR> <TD> flamingo </TD> <TD> 2 </TD> </TR> <TR> <TD> penguin </TD> <TD> 3 </TD> </TR> <TR> <TD> robin </TD> <TD> 2 </TD> </TR> </TABLE> </BODY> </HTML>