Go to ASP programming advanced (5): built-in object response

Source: Internet
Author: User
Author: cnbruce from: Blue ideal

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:
Run code
<Script language = vbs>
Randomize' first, randomize generates random seeds.
Suij = RND () 'and then assign the function value to suij.
Suij = Replace (suij, ".", "9") 'then converts the decimal point in the suij variable to 9
Suij = left (suij, 6) 'and then take the left 6 digits in the Variable
Document. Write "<body bgcolor = #" & suij & ">" 'is applied to the background color.
</SCRIPT>

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.
Run code
<Script language = vbs>
Do until choose = vbno
Value = int (41 * RND) + 60) 'generates a random number between 60 and 100.
Msgbox Value
Choose = msgbox ("roll again? ", Vbyesno)
Loop
</SCRIPT>
The random function is very useful in the examination system. Of course, you can also use it for your own lottery. Just click here to get the first color :)

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 <HTML>.

9, status. asp
Response. Buffer = true

For the response objectAttributeAndMethodFirst come here, with one leftData SetCookie.
Continue to the cookies lecture.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.