ServerSide Web Programming with Active Server Pages ASP
Server-Side Web Programming with Active Server Pages ASP Request Object - Part 1 1/5/2022 © 2000, 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. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 2
ASP Request Object • Handles client requests of the webserver--the heart of dynamic server-side content. • Makes available all the information a client provides when requesting a page or submitting a form. • Client passes these values via HTTP headers (e. g. , user agent information, cookies), values passed in the URL, and values passed via a form. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 3
ASP Request Object • Client. Certificates Collection – Values passed from the client's digital certificate. Used primarily for secure transactions/authentication. – Not covered in this class. • Cookies Collection – Info stored by the server in cookies earlier on the client are passed back in requests to the server's domain. – Covered in a later lecture. • Form Collection – Values entered into an HTML form are passed to the server in the HTTP header. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 4
ASP Request Object • Query. String Collection – Values appended to a URL – E. g. , http: //www. barnesandnoble. com/bookstore. asp? userid=22 W 29 R • Server. Variables Collection – Somewhat misnamed. Includes the various header values passed by the client, plus miscellaneous server-related values. – E. g. , User agent, Client's IP address, Server name, URL of current page – More later… 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 5
Request Object Example • HTML form submitted by client. . . <form action="eg. asp" method="post"> <p>Enter your name <input type="text" name="YOURNAME" size="30"></p> </form>. . . • ASP processing page on server. . . <% response. write "<p>The name you entered was: " & Request. Form("YOURNAME") %>. . . 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 6
HTTP Review • HTTP is the protocol that WWW requests are based upon. • HTTP specification defines how client requests content from a server and how a server returns a response to the client. • HTTP supports many request types: – PUT and DELETE not commonly used – GET and POST are primary types. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 7
HTTP Review • GET requests ask server to “get” a resource (indicated by a URL) on the server. • GET request may include additional parameters appended to the URL required by the resource. • POST requests “post” information to a server-typically HTML form variables. • Additional information normally passed by server to accessory programs (e. g. , ASP. dll) 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 8
HTTP GET Request To retrieve the resource at http: //www. host. com/path/file. asp Browser sends request to host www. host. com, port 80: GET /path/file. asp HTTP/1. 0 From: someuser@valtara. com User-Agent: Mozilla/4. 01 The server sends a response back to the client: HTTP/1. 0 200 OK Date: Fri, 31 Dec 1999 23: 59 GMT Content-Type: text/html Content-Length: 1354 [other header information…] <html><body><h 1>Happy New Millenium!</h 1> (more file contents) </body> </html> 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 9
HTTP POST Request To retrieve the resource at http: //www. host. com/post. asp Browser sends request to host www. host. com, port 80: POST /post. asp HTTP/1. 0 From: someuser@valtara. com User-Agent: Mozilla/4. 01 Content-type: application/x-www-form-urlencoded Content-length: 13 [mandatory blank line] name=Jane+Doe The server sends a response back to the client: HTTP/1. 0 200 OK Date: Fri, 31 Dec 1999 23: 59 GMT Content-Type: text/html Content-Length: 1354 [other header information…] <html><body>Your name is Jane Doe. </h 1> </body></html> 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 10
GET vs. POST Forms • Browsers default form methods to "GET". • But coders should use "POST" in most cases. – Limits on URL length limits value passing (approximately 2000 characters) – POST hides values from users – name/value pairs are sent in the body of the request instead of URL. – More attractive URLs – But be aware that values are not really hidden. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 11
ASP Request. Form • The Request. Form collection allows retrieval HTML form field name/value pairs sent to server by client via the HTTP post method. • For Request. Form to access HTML form data, set form’s “ACTION” attribute to “POST”. <form action="subscribe. asp" method="post"> • Several syntaxes for accessing values. Use first. Request. Form("my. Form. Variable") Request. Form. Item("my. Form. Variable") Request. Form(1) 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 12
Request. Form Example Input Page called input. htm: <html>. . . <body><form method="post" action="output. asp"> <P>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 "Your name is " & Request. Form("NAME") %> </body></html> 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 13
Manipulating Request. Form • Can iterate all form field names and values in the Request. Form collection. Dim o. Item For Each o. Item in Request. Form Response. Write "<p>" Response. Write "Name: " & o. Item & " " Response. Write "Value: " & Request. Form(o. Item) & "</p>" Next 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 14
Multivalue Form Fields • Providing an INPUT element same name attribute allows for multi-values. <INPUT type="checkbox" name=”MYCHK" value="Val 1"> type="checkbox" name=”MYCHK" value="Val 2"> type="radio" name=”MYRAD" value="Value 1"> type="radio" name=”MYRAD" value="Value 2"> • Radio and checkbox fields without value attributes return a value of "on" if checked. <INPUT type="checkbox" name=”MYCHK"> Returns: Request. Form("MYCHK") would return "on" if checked, else would return no value (would not be in the collection). 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 15
Multivalue SELECT Fields • <SELECT> element passing values directly <select name="multi" multiple> <option>Val 1</option> <option>Val 2</option> </SELECT> • <SELECT> element passing indirect values <select name="multi" multiple> <option value="12">First. Item</option> <option value="23">Second. Item</option> </SELECT> 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 16
Request. Form & Multivalue Fields • Multivalue form elements output as commaseparated values Response. Write Request. Form(”MYMULTI") Returns: 1, 2, 3 • Or can iterate values in multi-value form elements: for each s. Item in Request. Form(”MYMULTI") Response. Write s. Item & "<BR>" next Returns: 1/5/2022 1 2 3 © 2000, Valtara Digital Design/Blitzkrieg Software 17
Request. Form & Multivalue Fields • Or can quickly load multi-value fields into an array using the VBScript SPLIT function. Dim s. Array = SPLIT(Request. Form("MULTI"), ", ") For I = LBOUND(s. Array) to UBOUND(s. Array) … 'do something Next 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 18
Request. Form & Multivalue Fields • Count Property: Request. Form(”MYMULTI"). Count • If a field does not have multiple values, count is 1. If field does not exist, count is 0. • Use Count property to iterate multivalue fields for i = 1 to Request. Form(”MYMULTI"). count Response. write "Item: " & Request. Form(”MYMULTI")(i) & "<BR>" next RETURNS: Item: 1 Item: 2 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 19
Request. Form & Multivalue Fields • Can also treat <INPUT> textboxes as multivalue fields by giving each box the same name. Address 1: <INPUT type="text" name="Address"> Address 2: <INPUT type="text" name="Address"> RETURNS: Address Line 1, Address Line 2 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 20
Using Request. Form • Request. Form collection returns an empty value for undefined or undeclared form variables – e. g. , if a form variable does not exist and you ask for it, you receive an empty value. • OPTION EXPLICIT won't help with this because not Request. Form variables are not dim-able • Make form field names short, descriptive and uppercase to avoid problems. • You may need to implement special handling of carriage returns in <TEXTAREA> form fields. 1/5/2022 © 2000, Valtara Digital Design/Blitzkrieg Software 21
- Slides: 21