Introduction to ASP Programming (V): built-in objects response

Source: Internet
Author: User
Tags browser redirect character set execution header random seed range variable client
response| Programming | objects | Built-in objects by learning from the request object, you can learn that the request object is the information that the server side uses to get the client.
But is there a lack of server-side messaging to the client as the server interacts with the client? Object response is to hold this responsibility.
The object that is responsible for passing information to the user is response, which dynamically responds to client requests and returns dynamically generated response results to the client browser.
One, Response.Write
The Write method in response is the most frequently used one, write is written: writes the specified string to the current HTTP output.

1,write.asp


<%
Response.Write ("Hello,world" & "<br>")
Randomize
Response.Write "Any number is" &rnd () & "<br>"
%>




The information displayed after Response.Write can be enclosed in parentheses or written directly (note that there are spaces between the Response.Write).
Write string information or HTML code related, enclosed in quotes, and the ASP itself's function or variable is not needed, directly.
And the connection between string information, HTML code, function, or variable is a & number (for VBScript)

The above encounters a random function rnd (), which returns a value less than 1 but greater than or equal to 0.
Note that the random number generator is initialized with a Randomize statement without parameters before calling Rnd, which has a seed based on the system timer.
If the randomize is missing, the random seed cannot continue to be generated.

The following example uses a random function to produce a random background color effect:
<script language=vbs>
Randomize ' first Randomize produce random seeds
Suij=rnd () "then assigns the function value to the variable Suij
Suij=replace (Suij, ".", "9") ' then convert the variable suij decimal symbol to number 9
Suij=left (suij,6) ' re-take the left 6 bits in the variable
document.write "<body bgcolor=#" &suij& ">" is finally applied to the background color.
</script>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

Of course, to produce random integers of the specified range, use the following formula:
Int ((upperbound-lowerbound + 1) * Rnd + lowerbound)
Here, the upperbound is the upper bound of this range, and the lowerbound is the lower bound within this range.
<script language=vbs>
Do Until choose = vbno
Value = Int ((Rnd) + 60) ' produces a random number between 60 and 100.
MsgBox Value
Choose = MsgBox ("Roll again?", vbYesNo)
Loop
</script>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]
Random function in the exam system is very useful, of course, you can also be used to shake the prize yourself, take the color also points I can do

Second, Response.End
This is a commonly used method of response the WEB server to stop processing the script and return the current result, and the remaining content in the file will not be processed.
Mainly in a page of multi-functional ASP page, in order to let the program run to this, or down the program does not need to perform the display ...

2,end.asp


<%response.write Now ()
Response.End ' program execution shows this end
Response.Write Rnd ()%>




Three, Response.Clear
The main purpose of this method is to clear all the HTML output in the buffer, but the method clears the response body only and does not clear the response header.
The method and the end method seem to be the opposite, and ending returns the result above, while clear clears the above execution and returns only the following results.

3,clear.asp


<%response.write Now ()
Response.Clear ' All the above procedures are cleared
Response.Write Rnd ()%>




Here's an example to take a closer look at the 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 "Using the Clear method, the above program result will be cleared." "
Response.Clear ' empties the pages stored in the cache
Else
Response.Write "Using the End method, the following program will be deactivated." "
Response.End ' stops script processing immediately and prints the page in the cache
End If
%>
If you only see the "Refresh" link, the Clear method is already in effect <br>
<a href= "<%=filepath%>" > Refresh </a>




In the above program, when the username and password are cnbruce, the result of the clear method is found, whereas the other effect is another.

Four, Response.Redirect
The Redirect method is to have the browser redirect immediately to the URL address specified by the program. This is important to specify different pages for different customers depending on the customer's response, or to specify different pages based on different situations.
The method takes effect immediately and is not executed in subsequent scripts.

5,redirect.asp


<%response.redirect ("http://www.cnbruce.com/")%>




The above four use belongs to the response object several more important methods: Write, end, clear, redirect and so on Of course method also: AddHeader, AppendToLog, BinaryWrite, Flush ...
As I said earlier, an ASP object has object properties in addition to the object method

What are the attributes of the response object?

Five, Response.ContentType
The ContentType property specifies the HTTP content type of the server response. If ContentType is not specified, the default is text/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"%>




Six, Response.Charset
The Charset property attaches the character set name to the Content-type header in the Response object to set the file character encoding that the server responds to the client.

7,charset.asp


<% Response.Charset = "Big5"%>
Chinese display, but the use of Big5 traditional coding, so see is garbled.



Related Article

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.