Deep understanding of the ASP built-in objects response

Source: Internet
Author: User
Tags browser redirect character set execution flush header random seed range

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>

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>

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= "WEBJX" and pwd= "WEBJX" 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 WEBJX, 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.webjx.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.

Of course Response.ContentType and Response.Charset are less used. It's OK to add directly to the head header attribute.

Seven, Response.Expires

This property specifies how much time is available to buffer the stored page on the browser.

If a user returns to this page before a page expires, the page in the buffer is displayed.

However, if you set the response.expires=0, you can make the cached page expire immediately.

This is a more practical property, when the customer through the ASP's Landing page into the Web site, you should use this property to make the landing page expire immediately to ensure security.

8,expires.asp

<%
Response.Expires = 0
Response.ExpiresAbsolute = Now ()-1
Response.AddHeader "Pragma", "No-cache"
Response.AddHeader "Cache-control", "private"
Response.CacheControl = "No-cache"
%>

Eight, Response.Status

Sets the value of the status row that the server is responding to. Response.status= "Status Description string", the string can be a three-bit integer or a string of descriptive text, but must be anti-

9,status.asp

<% Response.Status = "401 Unauthorized"%>

Nine, Response.Buffer

The more important one, the value is true or false. This property indicates whether the page output is buffered.

Reference: When the page output is buffered, the server sends the response to the client browser only after all server scripts for the current page have been processed or the Flush or End method is invoked, and the server will not be able to set the buffer property after it has been sent to the client browser. You should therefore call Response.Buffer on the first line of the. asp file.

Response.buffer=true

For the properties and methods of the response object, there is a data collection, a cookie, that comes first. Go ahead and see the cookie lecture.

Examples of application problems

Form Outlook mail

<style>
input{border:1px solid Navy; width:150}
</style>
<script>
function Test () {
var Newtitle=title.value;
var Newcontent=content.value;
document.location.href= "mailto:" +aaa.value+ "cc=" +bbb.value+ "&bcc=" +ccc.value+ "&subject=" +NewTitle+ " &body= "+newcontent;
}
</script>
<pre>
<font color=red> recipient: </font><input name=aaa><br>
<font color=red>: </font><input name=bbb><br>
<font color=red> bcc: </font><input name=ccc><br>
<font color=red> Theme: </font><input name=title><br>
<font color=red> Content: </font></pre><p>
<textarea cols=30 rows=10 name=content></textarea><br>
<input Type=button value= "Send" >

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%>



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.