Introduction to ASP Basics seventh (ASP Built-in object response) _ Application Tips

Source: Internet
Author: User
Tags flush stop script

Below, we begin to learn another ASP's Response object.

Instead of obtaining client HTTP information, the Response object is used to control the information sent to the user, including sending the message directly to the browser, redirecting the browser to another URL, or setting the value of the cookie.

Syntax: Response.collection|property|method

One, property

1, Buffer

The Buffer property indicates whether the page output is buffered. 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 no longer 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.

2, Charset

The Charset property attaches the character set name to the back of the Content-type header in the Response object. For ASP pages that do not contain the Response.Charset property, the Content-type title will be: content-type:text/html.

We can specify Content-type headers in the. asp file, such as:

<% response.charset= "gb2312")%>

The following results are produced:

content-type:text/html; charset=gb2312

Note that the feature inserts it into the Content-type header, regardless of whether the character set represented by the string is valid. And if a page contains more than one tag that contains Response.Charset, each Response.Charset replaces the previous charsetname. In this way, the character set is set to the value specified by the last instance of Response.Charset in the page.

3, ContentType

The ContentType property specifies the HTTP content type of the server response. If ContentType is not specified, the default is text/html.

4, Expires

The Expires property specifies how much time is available to cache the paged memory on the browser. If a user returns to this page before a page expires, the page in the buffer is displayed. If you set response.expires=0, you can make cached pages 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.

5, ExpiresAbsolute

Unlike the Expires property, the ExpiresAbsolute property specifies the exact expiration date and time of the page that is cached in the browser. The page in the cache is displayed if the user returns to the page before it expires. If no time is specified, the home page expires at midnight on the same day. If no date is specified, the home page expires at a specified time on the day the script is run. The following example specifies that the page expires in 30 seconds on December 10, 1998 9:00.

<% response.expiresabsolute= #Dec 12,1998 9:00:30#%>

Second, the method

1. Clear

You can clear all of the HTML output in the buffer with the clean method. However, the clear method clears only the response body and does not clear the response header. You can use this method to handle error conditions. However, if Response.Buffer is not set to TRUE, this method will cause a run-time error.

2, End

The end method causes the WEB server to stop processing the script and return the current result. The remaining content in the file will not be processed. If the Response.Buffer is set to TRUE, the call to Response.End will buffer the output.

3, Flush

The Flush method immediately sends the output in the buffer. If Response.Buffer is not set to TRUE, the method causes a run-time error.

4, Redirect

The Redirect method causes the browser to immediately redirect to the URL specified by the program. This is also a method that we often use so that programmers can specify different pages for different customers depending on the customer's response, or different pages for different situations. Once the Redirect method is used, any response body content that is explicitly set in the page is ignored. However, this method does not send the client a different HTTP header for the page set, resulting in an automatic response body containing the redirected URL as a link. The Redirect method sends the following explicit caption, where the URL is the value passed to the method. Such as:

<% Response.Redirect ("www.jb51.com")%>

5, Write

The Write method is one of the most common methods we usually use to write the specified string to the current HTTP output.

Third, the collection

The Response object has only one collection--cookie

Cookies collection sets the value of the cookie. If the specified cookie does not exist, it is created. If it exists, the new value is set and the old value is deleted.

Grammar

Response.Cookies (cookie) [(key) |. Attribute]=value

The cookie here is the name of the specified cookie. If a key is specified, the cookie is a dictionary. attribute specifies information about the cookie itself. Attribute parameters can be one of the following:

If domain is specified, the cookie is sent to the request in the domain.

EXPIRES Specifies the expiration date for the cookie. To store cookies on the client disk after the session has ended, you must set the date. If the setting for this property does not exceed the current date, the cookie expires after the task ends.

HasKeys Specifies whether the cookie contains keywords.

If path is specified, the cookie is sent only to the request for that path. If this property is not set, the path to the application is used.

Now that we have learned the theoretical knowledge of all the attributes, methods, and sets of the Response object, the following authors will show you a simple procedure that you can use to deepen your understanding. First clip the following program into a notepad and save it as a asp7.asp. (Note that the space between < and% is removed!!!)

<% Dim user Dim Flag Dim pwd Dim Say response.buffer=true ' Open buffering page feature response.contenttype= ' text/html ' Response.
  charset= "gb2312" User=request.form ("username") pwd=request.form ("password") say=request.querystring ("Say")%> < form method= "POST" action= "asp7.asp" > < p> user name:< input type= "text" name= "username" size= "" >< b r> password:< input type= "password" name= "password" size= "" >< br> < input type= "submit" value= "Submit" N Ame= "B1" >< input type= "reset" value= "Cancel" name= "B2" ></p></form> <% If Say=1 then respons
E.write "Welcome scholar to visit!"
End If If say > 1 Then Response.Write "Welcome to the scholar's ASP website again!"
  End If If user= "ADM" and pwd= "Shusheng" Then response.expires=1 "setting the page expires after 1 minutes of storage in the browser's buffer.
  Flag=1 ElseIf user= "Guest" and pwd= "Guest" Then response.expires=0 ' causes cached pages to expire immediately. Response.Clear ' empties the pages stored in the cache flag=2 ElseIf user= "VIP" and pwd= "VIP" Then Response.Write "Welcome VIP to the scholar's ASP website" Flag=3 Else flag=0 Response.End ' immediately stop script processing and output the page end If Response.Write ' < p>< a href= ' asp7b.asp?flag= in the cache "&flag&" > Dynamic website Design skills--asp (7) Practice Practice </a>&l;  
  /p> "' Flag the value of the variable to asp7b.asp%> < p> Dynamic Web site design Skills--asp article (7) Practice </p> Save the following program as a asp7b.asp. <% Dim Saysay=request.querystring ("flag") Select case say case "1" Response.Redirect "Asp7.asp?say=1" case " 2 "Response.Redirect" asp7.asp?say=2 "case" 3 "Response.Redirect" asp7.asp?say=3 "case" 0 "Response.Redirect" asp7.a
 Sp?say=0 "End Select%>

Place the two programs under a WEB virtual directory with Execute permissions and Access asp7.asp in HTTP. In both programs we have fully utilized the two ASP-built objects that have been learned so far: Request and Response. When you first access the asp7.asp file, a Form appears on the page and prompts for the username and password, and if you enter the username: ADM, Password: Shusheng, the following page will appear:

What is this for? Because we set up the page cache, when we log in using the guest, the judge automatically clears all the pages stored in the cache before the results of the subsequent script execution are displayed.

So why did you just display a Form dialog when you first landed on the asp7.asp page? Because at this point the program determines that the user is neither ADM nor guest and VIP, executes Response.End, immediately displays the Form stored in the cache, and stops all subsequent script processing, including the display of pure HTML code.

Whether you log on to the page with ADM or guest or VIP, you can see a hyperlink that points to the file asp7b.asp, and when we click the hyperlink, Asp7.asp sends the value of the variable flag as an argument to the asp7b.asp, which is based on the value of the parameter. Judge, and use Response.Redirect to forcibly return asp7.asp according to different situations, and also send the value of a variable say as an argument to asp7.asp. In this way, asp7.asp will react differently according to this parameter.

As you can see now, we've only used two very simple programs, can do on the same page a variety of different display results, this is the charm of the ASP, you can use this model program as a basis for some changes to master the two built-in objects we learned.

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.