Previous Section Table of Contents Next Section

The Response Object

You've already seen a quick example of the Response object and its most important method: Write. The Response object is used to output information to the client Web browser, including text, cookies, HTML headers, and more. In an administrative script, you're not likely to use much more than Response.Write, and it's one of the two additional commands that I mentioned you'd have to learn.

Writing Output

Response.Write simply outputs text to the Web page. Think of it as a sort of MsgBox statement, or more accurately a WScript.Echo for Web pages. As you've seen, you can include functions and literal text, and Response.Write simply outputs whatever you tell it to.

Here's another example of how Response.Write works. Save this page as Response.asp in your computer's Web root folder, and access it via http://localhost/response.asp.


Response.Write Now & "<br><br>"

Response.Write "Hello!" & "<br><br>"

Response.Write "All done!"

Use View Source in your Web browser to see the final HTML that was transmitted to the browser.

TIP

The <br> tags I use in this example are HTML tags for a line break. They're similar to using vbCrLf in a regular script, and tell the Web browser to insert a carriage return and linefeed. Use two of them in a row, as I've done, to create blank lines. Using vbCrLf doesn't work in an ASP script, because Web browsers tend to ignore incoming carriage returns and linefeeds when they display HTML.


Saving Cookies

The Response object also allows you to save cookies to client computers. A cookie is a small collection of data, normally smaller than 1024 bytes in size. A cookie is a collection of crumbs (seriously), with each crumb representing one piece of data. So, if you want to save the user's name, that would be one crumb in the cookie, the user's last logon date might be another. In administrative scripts, cookies tend to have limited use. One potential use is in a Web-based wizard, such as a new user creation wizard. In that application, you might use a cookie to keep track of the settings the user enters on each page of the wizard; on the last page, you could then collect all that data together to create the new user account.

Response provides access to cookies through the Response.Cookies collection. It's simple to use; here's an example of setting two crumbs.


Response.Cookies("UserName") = "JohnD"

Response.Cookies("AcctExpires") = 0

The trick with cookies is that they have to be passed in the HTTP headers, not in the main HTML code. That means you have to set the cookies before any HTML is output to the browser. The following example works fine.


<%

Response.Cookies("WizardStep") = 2

Response.Cookies("UserName") = "DonJ"

%>

<HTML>

<BODY>

<!--Rest of HTML goes here-->

</BODY>

</HTML>

However, the following example would result in an error.


<HTML>

<BODY>

<%

Response.Cookies("RightNow") = Now()

%>

Thank you!

</BODY>

</HTML>

The error occurs because HTML has already been sent to the browser (and therefore the HTTP headers composed) by the time Response.Cookies is accessed. The following example will also result in an error, although for a slightly more subtle reason.


<%

Response.Write "Today is " & Now()

Response.Cookies("UserName") = "JohnDoe"

%>

<HTML>

<BODY>

Hello!

</BODY>

</HTML>

Why the error? Because when Response.Write is executed, IIS realizes that this is the first HTML being output to the browser. As such, IIS automatically finalizes the HTTP headers, sends an automatic <HTML><BODY> tag set, and then executes Response.Write.

TIP

Avoid confusion by never using Response.Write until after your <HTML> tag; and never use Response.Cookies unless it's before that <HTML> tag.


You'll see how to reread those cookies when I describe the Request object in the next section.

Ending

Sometimes, you may want to tell ASP to stop processing your script and wrap things up. You can! Simply execute Response.End. ASP immediately will send whatever output it has already generated and won't look at your script any further.

    Previous Section Table of Contents Next Section