1 / 33

Using the Request and Response Objects

8. Using the Request and Response Objects. Internet Programming Using VBScript and JavaScript. 8. Objectives. In this chapter you will: Retrieve information from a form using the Form and QueryString Collections Retrieve server variables using the request object

tallis
Download Presentation

Using the Request and Response Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8 Using the Request and Response Objects Internet Programming Using VBScript and JavaScript

  2. 8 Objectives • In this chapter you will: • Retrieve information from a form using the Form and QueryString Collections • Retrieve server variables using the request object • Redirect users to another page using the response object • Control output that is sent to the browser using the response object • Control the caching of content on a Web page using the response object

  3. 8 The Request and Response Objects • When a browser sends a request to a Web server, the request object can be used to retrieve information about that request • The browser request will include the date and time, the type of request (get or post), the page requested, and the HTTP version • The response object is used to send information to the browser • Some of this information identifies the server, such as the IP address of the server, the name of the operating system, and the Web server software version

  4. 8 Retrieving Information From a Form • The name and value are separated by an equal sign (=) • Each name and value pair is separated from the next by an ampersand (&) • The two methods that pass the form field information are the get method and the post method • The get method appends the name and value pairs to the URL with a question mark (?)

  5. 8 Retrieving Information From a Form • The request object is used to retrieve form information that has been submitted to the server • The request object contains a forms collection and a QueryString collection • If the form is sent using the get method, then the QueryString collection is used to retrieve the name and value pairs • If the form is sent using the post method, then the form collection is used to retrieve the name and value pairs

  6. 8 Retrieving Information From a Form • The name and value pairs are sent URL-encoded, which means that spaces are replaced with plus signs (+), and special characters are replaced with the percent sign (%) and the hexadecimal value for the character • In order to retrieve the name and value pairs as they were sent, you would have to split the name and value pairs, parse the name and values, then replace the plus signs and hexadecimal values with blank spaces and the special characters

  7. 8 The Form Collection • The form collection allows you to retrieve the names and values of fields from a submitted form • If the form field does not have a form field name specified in the form field tag, the value will not be sent • The Form Field button and the Reset button will never send a value • The Form Field button is usually used to call a function or submit a form • If a Submit button has a name but does not have a value, then the value will be assigned “Submit Query”

  8. 8 The Form Collection • You can detect if a user has completed a form by determining whether the form collection is empty • To retrieve a single field from the form collection, specify the request object name, the form object, and the name of the field, inside quotation marks

  9. 8 The QueryString Collection • The QueryString collection allows you to retrieve the name and values of fields from a submitted form • To retrieve the entire QueryString collection, specify the request object name and QueryString • All form fields with a name will be received in the QueryString collection, including the Submit button

  10. 8 The QueryString Collection • It is important to understand that the QueryString is also accessible through the server variables collection • To retrieve a single field from the QueryString collection, specify the request object name, the QueryString object, and the name of the field, inside quotation marks

  11. 8 Displaying the Form and QueryString Collections without the Form Field Names • To create a Web page that displays the names and values of fields without knowing the names of the fields, you have to use a For Each statement • The QueryString and form collections are child objects of the request object • You can use the For Each statement to loop through each of the form field objects in the collection, and retrieve the name of the form field and the value

  12. 8 Other Types of Form Fields • Text boxes are not the only type of field that forms can contain • Some of the other types of form fields include drop-down lists, radio buttons, and check boxes • Retrieving the values of these form fields is similar to retrieving the value of a text box • You can use the Form or QueryString collection to retrieve the values from these form elements

  13. 8 Drop-Down List Boxes • Drop-down list boxes are used to provide a specific list of choices, called options • The drop-down list box can be configured to allow users to select multiple options, or to limit them to one selection by using the multiple property in the select tag • There is no difference between the code used to display a text box and the code used to display the selection from a drop-down list box

  14. 8 Drop-Down List Boxes • The only difference between a text box and a drop-down list box is that the user can select more than one item from a drop-down list box if you specify the multiple property in the select tag • Unlike most arrays, the index number for a drop-down list box begins with one

  15. 8 Check Boxes and Radio Buttons • Check boxes and radio buttons are used to provide lists of choices, similar to a drop-down list box • Like text boxes, check boxes and radio buttons use the input tag • You can specify a different name for each check box, which will make it easier to retrieve individual check box • To specify a radio button form field, the type property is set to radio

  16. 8 Check Boxes and Radio Buttons • The radio button is also known as an option group because the user can only select one option from the group of radio buttons • For both check boxes and radio buttons, you need to include text to identify each option for the user • If a value is not specified, the value that is sent when the user submits the form is “on” • It is important to know that check boxes and radio buttons will not send a value to the server if none of the options are selected

  17. 8 Check Boxes and Radio Buttons • There is no difference between the code that displays a check box and the code that displays the results from a text box • This is because each check box and text box is identified by a unique name • If you assign the same name to multiple check boxes, such as chk1, the entire form collection will display the name and value for each check box selected • You can specify the number of values in a form field by using the keyword count

  18. 8 Check Boxes and Radio Buttons • The keyword count can be applied to form fields that are retrieved from the form collection and the QueryString collection • To display only those check boxes with a value, use a For Next control statement to loop through the results • For a group of radio buttons, you need only specify the field name of the radio button • You can only select one option from a group of radio buttons

  19. 8 Retrieving the Server Variables • The server variables are a collection within the request object • When a browser sends a request for a page to a Web server, it also sends information in the HTTP headers • This information, which includes the name of the page requested, the IP address of the destination Web server, and the IP address of the client, is used to process the request • The name of the server variable is enclosed within double quotes and is always written in all caps

  20. 8 Server Variables

  21. 8 Retrieving the Server Variables • The server variables can identify how much information is being sent to the server in the HTTP_CONTENT_LENGTH server variable • You can also access this same information using the totalbytes property of the request object • You can display individual server variable values, or display the entire collection of server variables • HTTP_USER_AGENT sounds like a great way to detect the browser version, it is often more useful to detect what features a browser supports

  22. 8 Displaying the Server Variables

  23. 8 Redirecting the Request to Another Page • You can use the refresh command in the META tag to redirect the user • When you use the refresh command, you specify the URL and the number of seconds to wait before redirecting the user to a new page • When you use server-side redirection, the client cannot stop the redirection from occurring • When you use the redirect method, users will not even be aware that they have been redirected, and therefore they cannot stop the redirection

  24. 8 Redirecting the Request to Another Page • To redirect the user, simply call the redirect method of the response object • The redirect method takes one parameter, the destination URL • You can combine the redirect method with other methods • For example, many companies have purchased multiple domains for marketing reasons • You might be assigned the task of redirecting these domains to specific directories or Web pages on your Web server

  25. 8 Controlling the Output to the Browser • Information that is sent to the browser is sent in the HTMP output stream, which is called the buffer • Sometimes you need to control when the information is sent to the browser • For example, the redirect method needs to send information in the HTTP header • If the HTTP header content had already been sent to the browser, an error would occur • Therefore, you needed to temporarily hold the content in the buffer by setting the buffer property to true

  26. 8 Controlling the Output to the Browser • When the buffer property is set to true, you can write ASP and HTML statements that are held in the buffer until you direct the buffer to release the output stream to the browser • The response object has a method called flush that releases the output stream to the browser, and another method called clear that erases the contents of the buffer without releasing it to the browser • The response object also has a method called end, which stops any HTML and ASP code from being released to the browser

  27. 8 Caching Web Pages • You can use the meta tag to control how the browser will cache a Web page • When you cache a Web page, you instruct the browser to store a local copy of the Web page and all of its contents in a temporary directory on the client • It is important to note that today many corporate networks use proxy servers that provide security functions • Proxy servers are also used to decrease network traffic to the Internet by caching Web pages

  28. 8 Caching Web Pages • You can set a property called CacheControl, which prevents the proxy server from caching the page • Some proxy servers can override this property and cache the pages anyway • The expires property of the response object is used to specify the number of minutes after which the cached version of a Web page should expire

  29. 8 Caching Web Pages • The date and time is entered using pound signs before and after the date and time • If you set the expires property to 0, or set the ExpiresAbsolute property to a date in the past, the browser will not be able to cache the Web page • However, some browsers do not allow you to override the cache settings that are set in the browser

  30. 8 Summary • Forms pass names and values to the Web server • The name and value are separated in the QueryString by an equal sign • Each name and value pair is separated from other name and value pairs by an ampersand • A question mark is used to append the QueryString to the URL • The get method sends the name and value pairs appended to the URL

  31. 8 Summary • The post method sends the name and value pairs as part of the HTTP request body • The names of fields must be specified in the form if you want to be able to retrieve the values using the request object • The request object is used to retrieve information from the browser • The request object receives the form field name and value pairs using the QueryString and form collections

  32. 8 Summary • The request object can retrieve the server variables that are sent in the HTTP header using the ServerVariables collection • The response object is used to send information to the browser • The redirect method will redirect the user to another Web page • The response object uses the write method to send output to the Web page • The output to the Web page can be controlled using the buffer property

  33. 8 Summary • The clear method clears the buffer, while the flush property sends the contents of the buffer to the browser • The end method stops the page from sending any further content to the browser • The response object is also able to configure the Web page to expire at an identified date and time • The CacheControl property is used to prevent proxy servers from caching Web pages • The expires property is assigned to a relative values in minutes

More Related