Internet

Active Server Pages Tutorial

ASP Objects

The Response Object

The Response object is used to send information or instructions from the server to the client (the browser).

Response Object: Methods
Response.AddHeader Adds a header, which sends information about the page to the browser. This could be the location of the page, the language used (for example, English), the server software, or the type of content.
Response.AppendToLog Adds an entry to the server’s log file. This can be used to track errors as well as requests for the page and users logged in at a given time.
Response.BinaryWrite Sends binary data to the client.
Response.Clear Deletes information from the buffer. This can be used to clear invalid information entered by the user before displaying an error page.
Response.End Terminates the execution of the page. This is generally used early in the page if an error of some sort is encountered.
Response.Flush Sends the contents of the buffer to the browser and empties the buffer. This can be used, for example, to display text or an image before the server processes a longer operation; using Response.Flush prevents a lengthy wait in front of a blank page.
Response.Redirect(“Page2.asp”) Redirects the browser to another page.
Response.Write Sends data to the browser.
Response Object: Properties
Response.Buffer Turns the buffer on or off. When the buffer is off, the server sends information to the browser as it is generated. When pages must process a lot of code, turning on the buffer prevents users from viewing a blank page until processing is complete.
Response.CacheControl Determines whether or not proxy servers can cache ASP-generated output.
Response.Charset Specifies the character set to be used by the page.
Response.ContentType Tells the browser how to interpret the contents of the page. This statement can be used, for example, to render a page as a Microsoft Word document.
Response.Expires Specifies how many minutes a browser should store the ASP page in cache.
Response.ExpiresAbsolute Specifies a date and time a cached ASP page will expire.
Response.IsClientConnected Determines if a client has disconnected from the server.
Response.PICS Sets a rating for content rating services.
Response.Status Indicates the status of a page request.
Response Object: Collections
Response.Cookies Creates and sets a value for a cookie on the client computer.

Setting the Content Type

The Response.ContentType statement specifies the content type of an ASP page. This can be text, an image type, a Microsoft Word document, or something else:

Response.ContentType = “text/html” Renders the contents as HTML. Since pages are normally rendered as HTML, including this statement isn’t necessary.
Response.ContentType = “text/plain” Renders the contents as plain text.
Response.ContentType = “image/GIF” Renders the contents as a GIF image file.
Response.ContentType = “audio/basic” Renders the contents as an audio file.
Response.ContentType = “video/MPEG” Renders the contents as an MPEG video file.
Response.ContentType = “application/x-pdf” Renders the contents as a PDF file. The browser automatically opens Acrobat Reader.
Response.ContentType = “application/vnd-ms.word” Renders the contents as a Microsoft Word document. The browser automatically opens Microsoft Word.
Response.ContentType = “application/vnd.ms-excel” Renders the contents as a Microsoft Excel document. The browser automatically opens Microsoft Excel.

The Response.ContentType statement must be included before any HTML code on the page:

<% Response.ContentType=”application/vnd-ms.word” %>

Displaying Data

The Response.Write statement renders data on the page. This data can be text, numeric or the value of a variable. The syntax for the Response.Write statement is as follows: Response.Write(“Show this text plus the following ” & variable) All data to be printed is enclosed in parentheses following the statement. Text is enclosed in quotation marks inside the parentheses, while variables are not. Multiple items are joined by the ampersand (&).

<% Dim Holiday Holiday=”Valentine’s Day” Response.Write(“Celebrate ” & Holiday & “!”) %>

In addition, you can format the data by including the appropriate HTML tags. Note, however, that the tags must be included within the quotation marks. This means that in order to format the value of a variable, you must include some extra characters to prevent errors:

<% Dim Holiday Holiday=”Valentine’s Day” Response.Write(“Celebrate ” & “” & Holiday & “!”) %>

Redirecting a Page

The Response.Redirect statement redirects the browser to another page:

<% Dim Holiday Holiday=”” If Holiday = “Valentine’s Day” then Response.Write(“Celebrate ” & “” & Holiday & “!”) ElseIf Holiday = “Christmas” then Response.Redirect(“Christmas.asp”) Else Response.Redirect(“Today.asp”) End If %>

Terminating a Page

The Response.End statement terminates an ASP page, stopping it from processing code. It is generally used to terminate a page when an error occurs, or when, for example, data fails some sort of validation:

<% Dim Holiday Holiday=”” If Holiday = “Valentine’s Day” then Response.Write(“Celebrate ” & “” & Holiday & “!”) ElseIf Holiday = “Christmas” then Response.Redirect(“Christmas.asp”) Else Response.End End If %>

Creating Cookies

Cookies are small text falls stored on the user’s computer. They contain information that is used the next time the user accesses the page. Cookies are often used to store user and session information and personalize pages. By default, cookies are deleted when the user closes the browser, but expiry dates can be set to store the information longer. The Cookies collection is used by the Response object to store information, and by the Request object to then read that information when it’s needed. You should set a cookie before any HTML code. The following example uses a cookie to track unregistered users’ access to a web site:

<% Dim varVisits Response.Cookies(“Visits”).Expires=Date+365 varVisits=Request.Cookies(“Visits”) If varVisits=”” Then Response.Cookies(“Visits”)=1 Response.Write(“Welcome!”) Response.Write(“You may access this site 3 times before registering.”) ElseIf varVisits>3 Then Response.Redirect(“register.asp”) Else Response.Write(“We’re glad to see you again!”) Response.Write(“You’ve visited us ” & varVisits & ” times.”) varVisits=varVisits+1 Response.Cookies(“Visits”)=varVisits End If %>

First, the value contained in the “Visits” cookie is assigned to the varVisits variable. Then the script checks whether the value of varVisits is nothing (“”), meaning the user has never accessed the site, greater than 3, or something else. For each of these conditions, specific actions are taken and, unless the user has exceeded 3 visits (in which case the page is redirected to a registration page), the value contained in the cookie is incremented by 1. Note: Some users disable cookies in their browsers or clear them periodically. In addition, cookies may not work depending on a computer’s firewall settings and the anti-virus software that’s installed. For these reasons, cookies can be an unreliable method of storing user information.