Monday, March 22, 2010

2.ASP Request Object The Request object is used to get information from a visitor.

________________________________________ Request Object When a browser asks for a page from a server, it is called a request. The Request object is used to get information from a visitor. Its collections, properties, and methods are described below: Collections Collection Description ClientCertificate Contains all the field values stored in the client certificate Cookies Contains all the cookie values sent in a HTTP request Form Contains all the form (input) values from a form that uses the post method QueryString Contains all the variable values in a HTTP query string ServerVariables Contains all the server variable values Properties Property Description TotalBytes Returns the total number of bytes the client sent in the body of the request Methods Method Description BinaryRead Retrieves the data sent to the server from the client as part of a post request and stores it in a safe array Collections: ASP Cookies Collection ________________________________________ The Cookies collection is used to set or get cookie values. If the cookie does not exist, it will be created, and take the value that is specified. Note: The Response.Cookies command must appear before the tag. Syntax Response.Cookies(name)[(key)|.attribute]=value variablename=Request.Cookies(name)[(key)|.attribute] Parameter Description name Required. The name of the cookie value Required for the Response.Cookies command. The value of the cookie attribute Optional. Specifies information about the cookie. Can be one of the following parameters: • Domain - Write-only. The cookie is sent only to requests to this domain • Expires - Write-only. The date when the cookie expires. If no date is specified, the cookie will expire when the session ends • HasKeys - Read-only. Specifies whether the cookie has keys (This is the only attribute that can be used with the Request.Cookies command) • Path - Write-only. If set, the cookie is sent only to requests to this path. If not set, the application path is used • Secure - Write-only. Indicates if the cookie is secure key Optional. Specifies the key to where the value is assigned ________________________________________ Examples The "Response.Cookies" command is used to create a cookie or to set a cookie value: <% Response.Cookies("firstname")="Alex" %> In the code above, we have created a cookie named "firstname" and assigned the value "Alex" to it. It is also possible to assign some attributes to a cookie, like setting a date when a cookie should expire: <% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2002# %> Now the cookie named "firstname" has the value of "Alex", and it will expire from the user's computer at May 10, 2002. The "Request.Cookies" command is used to get a cookie value. In the example below, we retrieve the value of the cookie "firstname" and display it on a page: <% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %> Output: Firstname=Alex A cookie can also contain a collection of multiple values. We say that the cookie has Keys. In the example below, we will create a cookie-collection named "user". The "user" cookie has Keys that contains information about a user: <% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %> The code below reads all the cookies your server has sent to a user. Note that the code checks if a cookie has Keys with the HasKeys property: <% dim x,y for each x in Request.Cookies response.write(" ") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write(" " next %> %> Output: firstname=Alex user:firstname=John user:lastname=Smith user: country=Norway user: age=25 ASP Form Collection ________________________________________ The Form collection is used to retrieve the values of form elements from a form that uses the POST method. Syntax Request.Form(element)[(index)|.Count] Parameter Description element Required. The name of the form element from which the collection is to retrieve values index Optional. Specifies one of multiple values for a parameter. From 1 to Request.Form(parameter).Count. ________________________________________ Examples Example 1 You can loop through all the values in a form request. If a user filled out a form by specifying two values - Blue and Green - for the color element, you could retrieve those values like this: <% for i=1 to Request.Form("color").Count Response.Write(Request.Form("color")(i) & " ") next %> Output: Blue Green Example 2 Consider the following form:

First name:

Last name:

Your favorite color:

The following request might be sent: firstname=John&lastname=Dove&color=Red Now we can use the information from the form in a script: Hi, <%=Request.Form("firstname")%>. Your favorite color is <%=Request.Form("color")%>. Output: Hi, John. Your favorite color is Red. If you do not specify any element to display, like this: Form data is: <%=Request.Form%> the output would look like this: Form data is: firstname=John&lastname=Dove&color=Red ASP QueryString Collection ________________________________________ The QueryString collection is used to retrieve the variable values in the HTTP query string. The HTTP query string is specified by the values following the question mark (?), like this: Link with a query string The line above generates a variable named txt with the value "this is a query string test". Query strings are also generated by form submission, or by a user typing a query into the address bar of the browser. Note: If you want to send large amounts of data (beyond 100 kb) the Request.QueryString cannot be used. Syntax Request.QueryString(variable)[(index)|.Count] Parameter Description variable Required. The name of the variable in the HTTP query string to retrieve index Optional. Specifies one of multiple values for a variable. From 1 to Request.QueryString(variable).Count ________________________________________ Examples Example 1 To loop through all the n variable values in a Query String: The following request is sent: http://www.w3schools.com/test/names.asp?n=John&n=Susan and names.asp contains the following script: <% for i=1 to Request.QueryString("n").Count Response.Write(Request.QueryString("n")(i) & " ") next %> The file names.asp would display the following: John Susan Example 2 The following string might be sent: http://www.w3schools.com/test/names.asp?name=John&age=30 this results in the following QUERY_STRING value: name=John&age=30 Now we can use the information in a script: Hi, <%=Request.QueryString("name")%>. Your age is <%= Request.QueryString("age")%>. Output: Hi, John. Your age is 30. If you do not specify any variable values to display, like this: Query string is: <%=Request.QueryString%> the output would look like this: Query string is: name=John&age=30 ASP ServerVariables Collection