Through learning the Request object, we can understand that the Request object is used by the server to obtain client information.
But as a server to interact with the client, is there still a lack of server to send information to the client? The object Response assumes this responsibility.
The object that is responsible for transmitting information to the user is Response. It can dynamically respond to client requests and return the dynamically generated Response results to the client browser.
1. Response. write
In Response, the write method is the most frequently used. write is to write the specified string to the current HTTP output.
1. write. asp
<% Response. write ("hello, world" & "<br> ") Randomize Response. write "any number is" & rnd () & "<br>" %> |
Response. write is followed by the displayed information, which can be included in parentheses or written directly (note that there is a space between response. write ).
The Written string information or HTML code is related and enclosed in quotation marks. ASP functions or variables are not required and can be used directly.
In addition, no matter the string information, HTML code, functions, or variables are connected using the & sign (for vbscript)
The precedingRandom Function rnd (), This function returns a value smaller than 1 but greater than or equal to 0.
Before calling Rnd, you must first use the Randomize statement without parameters to initialize the random number generator, which has a seed based on the system timer.
If Randomize is missing, the random seed cannot be generated.
The following example uses a random function to produce a random background color:
<Script language = vbs> <br/> Randomize 'First, Randomize generates random seeds. <br/> suij = rnd () 'then assign the function value to the variable suij <br/> suij = replace (suij ,". "," 9 ") 'then converts the decimal point in the suij variable to 9 <br/> suij = left (suij, 6) 'retrieve the left 6 digits of the variable <br/> document. write "<body bgcolor = #" & suij & ">" 'is applied to the background color. <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
Use the following formula to generate a random integer in the specified range:
Int (upperbound-lowerbound + 1) * Rnd + lowerbound)
Here, upperbound is the upper bound of this range, while lowerbound is the lower bound of this range.
<Script language = vbs> <br/> Do Until choose = vbNo <br/> value = Int (41 * Rnd) + 60) 'generates a random number between 60 and 100. <Br/> msgbox value <br/> choose = MsgBox ("Roll again? ", VbYesNo) <br/> Loop <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
The random function is very useful in the examination system. Of course, you can also use it for your own lottery. I just want to share the first prize.
2. Response. end
This is a common method of Response. The Web server stops processing the script and returns the current result. The remaining content in the file will not be processed.
It is mainly placed on a multi-functional ASP page. To enable the program to run here, or the program to run down, there is no need to execute the display ......
2, end. asp
<% Response. write now () Response. end' program execution ends till now Response. write rnd () %> |
3. Response. clear
This method is mainly used to clear all HTML output in the buffer, but this method only clears the response body without clearing the response title.
This method is opposite to the end method. The end method returns the above result at the end, while the clear method clears the above execution and returns only the following results.
3, clear. asp
<% Response. write now () Response. clear' all programs above have been cleared. Response. write rnd () %> |
Here is an example to take a closer look at end and clear.
4, end2clear. asp
<% Filepath = request. servervariables ("script_name ") User = Request. Form ("username ") Pwd = Request. Form ("password ") %><Form method = "POST" action = "<% = filepath %>"> Name: <input type = "text" name = "username"> <br> Pwd: <input type = "password" name = "password"> <br> <Input type = "submit" value = "submit"> </Form> <% If user = "cnbruce" and pwd = "cnbruce" Then Response. write "adopts the clear method, and the above program results will be cleared. " Response. clear' clear the pages stored in the cache Else Response. write "adopts the end method. The following program will be suspended. " Response. end' stop script processing immediately and output the page in the cache End If %> If you only see the "refresh" link, the clear method has taken effect. <br> <A href = "<% = filepath %>"> refresh </a> |
In the above program, when the user name and password are the same as cnbruce, the processing result of the clear method will be found, and vice versa is another effect.
4. Response. redirect
The Redirect method allows the browser to immediately Redirect to the URL address specified by the program. This is important when you specify different pages for different customers based on different responses, or specify different pages based on different situations.
This method takes effect immediately, and subsequent scripts are not executed.
5. redirect. asp
| <% Response. redirect ("http://www.cnbruce.com/") %> |
The above four are important to use the Response object.Method: write, end, clear, redirect, etc.Of course, the methods include AddHeader, AppendToLog, BinaryWrite, Flush ......
As mentioned earlier, an ASP objectObject MethodAndObject Attributes
The response object'sAttributeWhat are them?
5. Response. ContentType
The ContentType attribute specifies the HTTP content type of the server response. If ContentType is not specified, the default value isText/html.
6. contenttype. asp (non-text/html)
<% Response. ContentType = "text/HTML" %> <% Response. ContentType = "image/GIF" %> <% Response. ContentType = "image/JPEG" %> <% Response. ContentType = "text/plain" %> <% Response. ContentType = "image/JPEG" %> |
6. Response. charset
The Charset attribute attaches the character set name to the end of the content-type title in the Response object to set the file character encoding that the server responds to the client.
7. charset. asp
<% Response. charset = "big5" %> Chinese characters are displayed, but big5 is encoded in Traditional Chinese, so garbled characters are displayed. |
Of course, Response. ContentType and Response. charset have fewer applications. Simply add the header property to the header.
7. Response. expires
This attribute specifies the cache storage page on the browser, and the expiration time.
If you return to this page before a page expires, the page in the buffer zone is displayed.
However, if response. expires is set to 0, the cached page can expire immediately.
This is a more practical attribute. When you enter the WEB site through ASP login page, you should use this attribute to immediately expire the login page to ensure security.
Reference a classic example
8, expires. asp
<% Response. Expires = 0 Response. Expiresabsolute = Now ()-1 Response. AddHeader "pragma", "no-cache" Response. AddHeader "cache-control", "private" Response. CacheControl = "no-cache" %> |
8. Response. status
Set the value of the Status line to be responded by the server. Response. status = "status description string", which can be a three-digit integer or a string of explanatory text, but must be prevented before
9, status. asp
| <% Response. Status = "401 Unauthorized" %> |
9. Response. buffer
The value is true or false. This attribute indicates whether to buffer page output.
Reference: When the buffer page is output, the server sends the response to the client browser only after all the server scripts on the current page are processed or the Flush or End method is called, after the server sends the output to the client browser, the Buffer attribute cannot be set. Therefore, Response. Buffer should be called in the first line of the. asp file.
For the Response objectAttributeAndMethodFirst come here, with one leftData SetCookie.
Continue to the cookies lecture.
Application Example
Form to send OutLook mail
<Style> <br/> input {border: 1px solid navy; width: 150} <br/> </style> <br/> <script> <br/> function test () {<br/> var NewTitle = title. value; <br/> var NewContent = content. value; <br/> document. location. href = "mailto:" + aaa. value + "? Cc = "+ bbb. value + "& bcc =" + ccc. value + "& subject =" + NewTitle + "& body =" + NewContent; <br/>}< br/> </script> <br/> <pre> <br/> <font color = red> recipient: </font> <input name = aaa> <br/> <font color = red> run the following command: </font> <input name = bbb> <br/> <font color = red> BCC: </font> <input name = ccc> <br/> <font color = red> subject: </font> <input name = title> <br/> <font color = red> content: </font> </pre> <p> <br/> <textarea cols = 30 rows = 10 name = content> </textarea> <br/> <input type = button value = "send" onClick = "test () "> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]
ASP
<% Submitname = request. form ("submit ") If submitname = "submit" then Email = request. form ("email ") Cc = request. form ("cc ") Subject = request. form ("subject ") Body = request. form ("body ") Response. redirect ("mailto:" & email &"? Cc = "& cc &" & subject = "& subject &" & body = "& body) Else %> <Form name = "form1" method = "post" action = "email. asp"> Email: <input name = "email"> <br> Cc: <input name = "cc"> <br> Subject: <input name = "subject"> <br> Body: <input name = "body"> <br> <Input type = "submit" name = "submit" value = "submit"> </Form> <% End if %> |