When the user fills in the page
Form>
...
...
For help go to the Help Page
...
In this case, press the HELP button on the form to send a pair of name / value "Page = Help" in the Request.Form collection. However, pressing the hyperlink may also send the name / value "page = help", but this time is in the querystring collection. To access this value, you can use a special feature of the ASP Request object: strpage = request ("page")
This will search all sets in sequentially - QueryString, Form, Cookies, ClientCertificate, ServerVariables until the name of the first match value is found. This is lower than that of directly accessing appropriate collection efficiency, and is unsafe, unless it is absolutely guaranteed that this value does not appear in another collection.
For example, it may be desirable to collect the name of the web server that meets the client request, which is implemented in the Request.ServerVariables collection in each query. However, if any other collection also includes a value called "server_name" (remember the key name is not case sensitive), when using Request ("Server_Name"), the result is the result of the error. Using the REQEUST.SERVARVARIABLES ("Server_Name" syntax, we will be difficult to track.
All in all, using the "Search All Collection" technology should be taken particularly careful, and only when there is no other technique to provide the results you need.
g) Access to other collections
In this section of this chapter, the Form collection has been discussed, which may be one of the most used. However, all of these techniques are equally applicable to other objects. Includes those set by the Request object (ie form, querystring, cookies, servervariables, and clientpertate), and cookies provided by the Response object (and other objects that will be encountered in the next chapter).
We will briefly understand how a value enters a queryString collection, and its advantages and deficiencies. However, this two cookies sets have additional features that can be easier to use cookies, discuss this content below.
Access and update cookies collection
The value of cookies is much more complicated than other values of the ASP (such as Form and ServerVariables). Cookie is a small piece of text stored on the client system by the browser and serve the servers in the domain that are applied each time.
The ASP makes it easier to apply cookies, and you can get all the cookie values emitted with the request from the cookies collection of the Request object, and create or modify the cookie, send back to the user from the Cookies collection through the Response object.
The cookie contains information that can be constructed in two ways, and the single-valued cookie provides its value to the code through a general class ASP collection. However, each member of the collection may also be a collection, including the cookie of this information, by the multiple-value cookie.
Creating a single-value cookie is simpler, as shown below:
Response.cookies ("item-name") = "item-value"
Create a multi-value cookie, you can use the following command:
Response.cookies ("item-name") ("sub-item-name") = "Sub-item-value"
Set the domains and paths of the cookie app and its validity period, we use:
Response.cookies ("item-name"). Domain = "domain-url" response.cookies ("item-name"). Path = "Virtual-path"
Response.cookies ("item-name"). Expires = # Date #
Typically, the customer only disrespects the server when requested by the page in a directory in the cookie. By specifying the PATH property, you can specify where this cookie is legal, and this cookie will send it with the request. If the cookie is sent with the page requesting the entire site, set the PATH to "/".
If the Expires property is not set, the Cookie will be automatically eliminated when the current browser instance is turned off.
Note that when we send any output to your browser, we have created a cookie. Because these cookies are part of the page HTTP header.
In ASP 3.0, the buffered default state is open, and no output is sent unless you use Response.flush to specify this work or the page has arrived at the end. This means that creating cookie's code can be executed anywhere on the page until any output "refresh" (FLUSH) to the client, it can be performed.
To read existing cookies, use the Request.Cookies collection. You can access the items alone, and the method is similar to the method used when you create them.
StrsingLevalue = Request.Cookies ("item-name")
STRSUBITEMVALUE = Request.Cookies ("Item-name") ("Sub-item-name")
Note that the Request.Cookies collection (like all other Request collection) is read-only. The Response.Cookies collection is only written, in fact, you can access a series of cookies in this collection instead of their value.
Traversing cookies collection
To make it easier to use the cookies collection, additional properties named Haskeys can be used. If the visiting cookie itself is also a collection, that is, it is a multi-value cookie, which will return True. Use the Haskeys property to traverse the full Request.Cookies collection to get all the cookies lists and their values.
For Each Objitem in Request.cookies
If Request.cookies (Objitem). Haskey Then
'Use Another for Each To Itereate All Subkeys
For Each Objitemkey in Request.Cookies (Objitem)
Response.Write Objitem & "(" & objitemkey & ") =" _
& Request.Cookies (ObjitemKey) & "
"
NEXT
Else
'Print Out the cookie string as normal
Response.Write Objitem & "=" & required.cookies (objitem) & "
"
END IF
NEXT
This is very similar to complex code for extracting multiple values from the Request.form collection. However, here you can use the Haskeys property to determine if each entry is a collection. In the Form example, you must query the Request.form (item_name) .count property, because the Form collection (and all the other collections outside Cookie) cannot be a real collection. The ASP just did the "behind the scene" and got a value of each multi-purpose collection. Differences in Form and QueryString
After understanding the technology of accessing various ASP collections, you need to solve another problem is: What is the difference between Form and QueryString collections? If you are ready to use ASP, there is no doubt that this difference should be clear, but you need to refer to HTTP's way to re-understand and understand them.
Request page or other resource from the web server via HTTP, there are two universal methods. You can use the GET method to get resources directly, or you can pass the value to the corresponding resources using the POST. The GET method is default, you can see an instance of an HTTP request in front of this chapter:
7/8/99 10:27:16 Sent Get Get /Store/download.asp http / 1.1
If one or more paired names / values are attached to the URL of the request page, turn it into a request query string and is provided to the ASP page in the queryString collection. Click the hyperlink of web pages, email messages, or other documents, or enter your address in the browser's address bar and press Enter, or click the Links or Favorites button in your browser. All of this uses the GET method.
Therefore, the only way to transmit the value to the ASP in these actions is to attach the value to the URL through the queryString collection.
The value in the request.QueryString collection is also accessed, the same work in the form of the Form collection instance you see. Combination of URL and query strings:
Http://mysite.com/process_page.asp?firstname=priscilla&lastname=descartes
You can use the following way to access the values provided in the querystring collection:
StrfirstName = Request.QueryString ("firstname") 'Return "Prismilla"
StrlastName = Request.QueryString ("Lastname") 'Return "Descartes"
Strraw = request.queryString
'Return "firstname = priscilla & lastname = descartes"
Form GET and POST method
When using the
When this request arrives at the web server, its value is provided by the ASP's request.QueryString collection. However, if the Method property is set to "POST", the browser packages the value package into the HTTP header of the sending server, supplied to the ASP via the Request.form collection.
By, you can use the Post method in all HTML forms. However, there is a certain limit for the URL string of the browser or server. Therefore, a long string may cause overflow and characters of some strings to be cut off. At the same time, the query string appears in the address bar of the browser and all saved links and favorites. Not only that, but also revealing the value that does not want to display in the HTTP request through the web server, which may also have a log file of your server and other routing servers. The value in the HTTP request header is very visible and does not appear in the log file. Small problems you need to pay attention to using the POST method is that when the user re-downloads
Another point is that the combination of the URL and the query string cannot contain any spaces or other illegal characters, otherwise, Navigator and some other browsers will have problems. Illegal characters are portions that are used to secure the URL and query strings, such as "/", ":", "?" And "&" (IE can automatically convert space to the correct format - plus " ", But other illegal characters cannot be processed). ASP server object provides URLENCode method to process this transformation, and later discuss the relevant content.
View Request and Response object content
At present, some of the theoretical issues are mainly discussed, and there is no special example. It is closely related to each other in most cases that have been discussed. However, this book provides a series of examples of examples for this chapter, which shows most of the req Request and Response objects. Applications can understand these pages and can be modified to use them as a test example.
The sample samples of this chapter and all other chapters are provided to the user, and can be downloaded from the Wrox Press Site Site.
http://webdev.wrox.co.uk/books/2610
http://www.wrox.com/store/details.asp?code=2610
You must first install the instance in subdirectory within the WEB server wwwroot, then use the browser to access the Chapter02 subdirectory, use:
http: //your_server_name_or_ip/subdirectory_name/chapter02/default.asp
Here Your_server_name_or_ip / subdirectory_name is the local path to install the download file.
1, see the Request object member
This provides a menu containing the page to test the Request and Response objects, first select Using The Request Object, as shown below:
The figure below shows an instance of an HTML form that contains some pre-set values, you can edit these values according to your own ideas, then click the "Submit" button.
This will open a page, as shown in the following figure, display the full content of the collection and Totalbytes properties. The first screen is displayed for Form, QueryString, and Cookies collection.
Note that if the value of the HTML control is edited in the form page, different values may be displayed on the page on the reader's computer for the cookies collection and other collections.
You can see how the value of the HTML control on the form from the "Form Collection" segment is indicated in the ASP's Request.form collection. You can also test and detect with the original
A list of all ServerVariables collection members, and their values are available in the back appendix. However, these members can be seen from the client from the client when the request page is discussed previously. When the request is received, the web server also adds some of its own values to the collection, as you can see above, as you can see.
1) How is the page work?
In order to create this page, the exact same code seen in front of this chapter in the discussion of the Form set and how to access its value. For example, traverse all sets (outside of Request.cookies), use:
For Each Objitem in Request.collection_name
Response.Write Objitem & "=" & required.collection_name (objitem) & "
"
NEXT
Traverse the cookies collection, you can use:
For Each Objitem in Request.cookies
If Request.cookies (Objitem). Haskeys then
'Use another for ... Each to Itereate All Keys of Dictionary
For Each Objitemkey in Request.Cookies (Objitem)
Resonse.Write Objitem Objitem & "(" & objitemKey & ") =" _
& Request.Cookies (ObjitemKey) & "
"
NEXT
Else
'Print Out the cookie string as normal
Response.Write Objitem & "=" & required.cookies (objitem) & "
"
END IF
NEXT
To get TOTALBYTES properties, you can use it simply:
Request.totalbytes = <% = request.totalbytes%>
Readers should notice that certain values that appear in both collections are not directly obtained from the HTML controls of the form. QueryString set includes two values called Chapter and Sample, as shown below:
To create these two values in the request, attach a string on the URL of the form of the action attribute, which is acceptable. The working mode is similar to the HREF attribute attached to a element. The value of the query character appears in the querystring collection, and the value of the POST has appeared in the Form collection.
To prevent an error in the browser in non-IE class, the space in the query string must be replaced by the plus " ", and the reader will see more in the Urlencode method of the Server object in Chapter 4. 2) Create a client's cookie
In order to ensure that at least some value appears in the Request.Cookies collection, add some client script code to the original
This is a client code for setting the cookies property of the document object when the form is loaded:
Documet.cookie = 'visitcount = Visits = 3 & LastDate = 6% 2F4% 2F99 10% 3A10% 3A13 AM';
// ->
Script>
In addition, the content must be encoded so that it can be properly transmitted to the server (the same rule also applies to the query string to the URL). In Chapter 4, when discussing the Urlencode method of the Server object, the reader will understand more details.
2, view members of the response object
Go back to the original default.asp page of Chapter02 instance, this time you select the "Using The Response Object" link, this page displays the content of the Response object, and provides a link to all Response object methods.
The following figure is the screen that uses the browser Netsape Communicator 4.61 to prove that the use of pure server and cross-platform compatible technologies. It should be noted that the cookies collection is established for the Response object, only the name of the cookie is displayed without the value of its value. When you browse this page, you may not get a cookie or you get a cookie that is different from this page.
Various response properties illustrate some information that will be used to create an HTTP header. Other parts of the HTTP header page (HTML and text content) are sent to the client. Some of these properties and all the methods of all Response objects have a link, allowing the reader to open another page to display its usage. We will return to these pages later.
The attribute in the page is created by reading the corresponding attributes and inserted into the page. Since these are dynamic links, they are selected by elements.
= <% = Resposposne.cacheControl%>
Linking to each method is to pass the "link element, the only complex part of the page is the Response.Cookies collection. Read the value in the Request.Cookies collection by just accessing cookies. When accessing the Response.cookie collection, you must end all references to it before sending any output to the client. Therefore, the upper part of the page is placed in a local string variable through the traversal set.
Strcookies = ""
'We can only read the key names and not the value of the value
'The response.cookies collection is' Write ONLY'
For Each Objitem in Response.cookies
If Response.Cookies (Objitem) .Haskeys the'USE Another for Each To Iterate All Subkeys
For Each Objitemkey in response.cookies (Objitem)
Strcookies = strcookies & objitem & "(" & objitemkey & ")
NEXT
Else
'Print Out the cookie as Normal
Strcookies = strcookies & objitem & "
"
END IF
NEXT
The result is then inserted on the appropriate point of the page.
Is A Write-Only Collection So The VALUES Cannot Be Displayed i>
<% = strcookies%>
Use of cookies in ASP
In the page, some collection, attributes, and methods have been linked to other pages, used to display the details of the REQUEST and RESPONSE objects, we will study these content in the rest of this chapter, we will learn those provided Various techniques for the collection, methods, and attributes used by the ASP code.
In front of this chapter, I have seen how to create and read cookies in front of this chapter. When you click on any of the "cookies" links above the two pages above, this page contains some values for setting three cookies. The ASP code is displayed on the page, as shown below:
When you click on the link of "Show Cookies", the content of the cookie is displayed. This is obtained by traversing the Request.Cookies collection, which is identical to the way used in the previous page, as shown below:
This screen map shows the result of the code that runs the cookie value before running. You may see cookies that have been stored in your computer system. However, if the browser is closed now, then the browser is reopened, and then run the page of the cookie, all cookies are gone, this is because only this timedCookie has the validity setting, and other in the browser shut down, Automatically disappeared.
1) Details of the user in cookie
You can store these two types of values using cookies: When the browser is turned off, we don't want to save values (such as users' registration information), and the value to be retained when the user accesss the site. In each case, the value of cookies is available for ASPs requested from each page from the user browser.
However, it is necessary to remember that cookies will be sent to the server only when requesting a request within the virtual path (PATH) in the cookie. By default, if the value of PATH is not set in the cookie, its value is the virtual path to create a cookie page. In order to make a cookie to all pages of a site, you need to use path = "/".
Here is an instance, from the custom Login page, store the user's registration information in a cookie, because there is no application validity period, the cookie value is only closed before this browser: ...
Request.cookies ("UID") = "<% = Request (" UserName ")%>"
Request.cookies ("PWD") = "<% = Request (" password ")%>"
Request.cookies ("User"). Path = "/ adminstuff" 'Only Applies To Admin Pages
...
Now, this cookie can be found in each page requesting from the Adminstuff directory or its subdirectories. If it does not exist, you can redirect the user to the registration page:
IF (Request.Cookies ("UID") <> "alexhomer") _
Or (Request.Cookies ("PWD") <> "secret") THEN
Response.Redirect "Login.asp? Username =" & Request.Cookies ("UID")
END IF
...
Since the user name in the cookie is placed in the URL query string of Response.Redirect, if an error occurs when the password input is input, it is desirable that the user does not have to retrore the user name, you can use it in the login.asp page:
Value = "<% = Request.QueryString (" UserName ")%>>
Form>
2) Modify existing cookies
You can use ASP to modify existing cookies, but you cannot only modify a value in the cookie. When updating a cookie in the Response.Cookies collection, the existing value will be lost. We can create a cookie with the following code, you can use:
Response.cookies ("STARTDATE") = DTMStart
Response.cookies ("Visitcount") ("Lastdate") = now
Response.cookies ("Visitcount") ("Visits") = cstr (intVisits)
Response.cookies ("Visitcount"). Path = "/" 'Apply to Entire Site
Response.cookies ("Visitcount"). Expires = DateAdd ("M", 3, NOW)
If you want to update the value of Visits and LastDate, you must do not need to change all the values, then rewrite the entire cookie:
DATDTART = response.cookies ("STARTDATE")
Intvisits = response.cookies ("Visitcount") ("Visits")
Response.cookies ("STARTDATE") = DTMStart
Response.cookies ("Visitcount") = NOWRESPONSE.COOKIES ("Visitcount") = cstr (intVisits)
Response.cookies ("Visitcount"). Path = "/"
Response.cookies ("Visitcount"). Expires = DateAdd ("M", 3, NOW 1) and for almost all other response methods and properties, should be written in any content (ie open tag or any text) Or other HTML completed this work before responding.