ServerSide Web Programming with Active Server Pages ASP
Server-Side Web Programming with Active Server Pages ASP Request Object - Part 2 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 1
ASP Intrinsic Objects • • • Request - Exposes what client sent to server. Response - Collects output back to client. Application - Represents the ASP page itself. Session - represents a continuity of connection. Server - provides services to the application. Object. Context - For MTX and transactions. 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 2
ASP Request. Query. String • Used to retrieve information sent by client using GET method of an HTML form or data appended to the URL. • Generally used with URL querystring, not GET method on forms. <A HREF="out. asp? FNAME=Sue&LNAME=Jones"> • URL querystring can include up to approx. 1800 characters (depends on client) 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 3
Request. Query. String via Form GENERALLY DON’T USE GET WITH FORMS, USE POST Input Page called input. htm: <html>. . . <body><form method="get" action="output. asp"> <P>Please enter your name: <input type="text" name="NAME"><input type="submit" value="submit"></P> </form> </body></html> Output ASP Page called output. asp: <html>. . . <body> <% response. write "Name: " & Request. Querystring("NAME") %> </body></html> 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 4
Request. Querystring via URL Input Page called input. htm: <html>. . . <body> <p><a href="output. asp? VAL=234">Pass</a> the value 234 </p> </body></html> Output ASP Page called output. asp: <html>. . . <body> <% response. write "The value you pass is " & Request. Query. String("VAL") %> </body></html> 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 5
URL/Query. String Syntax & ? Delineates parameter/value pairs Delineates beginning of the querystring after the page address portion of URL % Used to encode in special characters using ANSI code in hexadecimal + Used for encoding spaces test. asp? VAL 1=Test&VAL 2=Test+Test&VAL 3=Bang%21 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 6
Manipulating Request. Querystring • Can iterate all form field names and values in the Request. Querystring collection. for each item in Request. Query. String response. write "Name: " & item & " " response. write "Value: " & Request. Query. String(Item) & "<BR>" Next 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 7
Request. Query. String & Multivalue Fields <A href="output. asp? MULTI=1&MULTI=2"> • Default output is comma-separated values Response. Write Request. Query. String("MULTI") RETURNS: 1, 2 • Iterate values in multi-value form elements: for each s. Item in Request. Query. String("MULTI") Response. Write s. Item & "<BR>" next RETURNS: 1 2 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 8
Request. Query. String & Multivalue Fields • Count Property: Request. Query. String("MULTI"). Count • If a field does not have multiple values, count is 1. If parameter does not exist, count is 0. • Use Count property to iterate multivalue fields for i = 1 to Request. Query. String("MULTI"). Count Response. write "Item: " & Request. Query. String("MULTI")(i) & "<BR>" next RETURNS: Item: 1 Item: 2 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 9
Using Request Data Wisely Avoid fetching the same date from the Request object more than once. Save the value to a variable and use the stored value. It speeds up code significantly. GOOD! BAD! Dim i. My. ID=rstemp("ID") if i. My. ID=234 then. . . end if if i. My. ID 235 then. . . end if 9/2/2021 if rstemp("ID")="234" then. . . end if if rstemp("ID")="235" then. . . end if © 1999, Valtara Digital Design/Blitzkrieg Software 10
Server. URLEncode Method • Use Server. URLEncode to encode URL querystrings. • Encodes in ANSI hexadecimal format strings that need to be included in the querystring portion of a URL. Server. URLEncode("http: //deathstar/test. asp? VAL 1= Bang!") Returns http%3 A%2 F%2 Fdeathstar%2 Ftest%2 Easp%3 FVAL 1%3 DBang +Bang%21 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 11
Using The Request Objects • Request collections return empty values for undefined or undelared variables – e. g. , if a form/querystring variable does not exist and you ask for it, you receive an empty value. • OPTION EXPLICIT won't help with this because not dim-able • Make form field names short, descriptive and uppercase to avoid problems. Especially important for querystring variables. 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 12
Using the Request Objects • Use URLs with querystrings to make your ASP pages bookmarkable. • Many ASP applications may use both Form and Querystring variables to achieve specific results. • A given page should not mix using both Form and Querystring variables (mixing HTTP GET and POST) on the same page. 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 13
Self-Referencing ASP Pages • Best practice for ASP pages is to merge multiple pages into a single page, e. g. , input form and output results in same ASP page. • Use hidden form fields and/or querystring variables along with IF…THEN or SELECT CASE to determine which view to display. • Display entry page by querying an undefined variable you define in other views. 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 14
Self-Referencing ASP Page 1 <html>. . . <body> <% IF Request. Form("VIEW") <> "Out" THEN %> <form method="post" action="default. asp"> <P>Enter name: <input type="text" name="NAME"> <input type="hidden" name="VIEW" value="Out"> <input type="submit" value="submit"></P> </form> <% ELSE %> <P>You Entered: <% = Request. Form("NAME") %></P> <a href="default. asp">Try Again</a> <% END IF %> </body></html> 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 15
<html>. . . <body> <% SELECT CASE Request. Form("VIEW") CASE 1 'output 1 %> <P>You Entered: <% = Request. Form("NAME") %></P> <P><a href="default. asp">Try Again</a></P> <form method="post" action="default. asp"> <input type="hidden" name="VIEW" value="2"> <input type="submit" value="Get Date"></form> <% CASE 2 ' output 2 %> Today's Date is <% = date() %> <P><a href="default. asp">Home</a></P> <% CASE ELSE 'input form %> <form method="post" action="default. asp"> <P>Name: <input type="text" name="NAME"> <input type="hidden" name="VIEW" value="1"> <input type="submit" value="submit"></P> </form> <% END SELECT %> </body></html> 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 16
Why Self-Referencing ASP Pages? • Entire page and all its logic is cached together on the webserver -- faster performance. • Simplifies coding by putting input/output in same place. • Code maintainability 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 17
Request. Cookies • Allows ASP pages to access the values of cookies and cookie dictionary items. • Response object has related capabilities to write cookie information. • More about this later in class. . . 9/2/2021 © 1999, Valtara Digital Design/Blitzkrieg Software 18
- Slides: 18