Sending ASP Results to the Browser- The Response Object/Class

 

Introduction.  You should all be at least somewhat familiar with the idea of objects and classes from previous Visual Basic or C++ courses.  VBScript has several built-in classes that you can instantiate with particular objects of the class.  The Response class is one of the most important classes and it is used to send output to the client.  Typically, this means taking the results of some VBScript and converting it into HTML so that it can be shown through a browser.

As you likely remember, classes have methods and properties.  Therefore an instance of a class, an object, also has methods and properties.  Remember that properties and methods are invoked using the .  notation so that they will be of the form  Response.Method or Response.Property.  The following table gives a brief overview of the Response class.  We will describe the methods and properties in more detail and develop some examples later in this page.

 

Method Property Description
Write   This sends data to the client's web browser to be displayed as part of a web page.
  Buffer This can be true or false depending on whether you want the results to be accumulated into a buffer and sent all at once to the client.
Clear   Assuming that buffering is turned on or true, this will cause the buffer to be wiped out.
Flush   Assuming that buffering is turned on, this will cause the buffer to be wiped out but it first sends the contents of the buffer to the client.
End   This terminates execution of a script.  If Response.Buffer = True then the contents of the buffer are first sent to the client.
  Expires This allows you to specify how many minutes before a cached page expires.  An example of this relates is when a user leaves your page and then returns.  If the elapsed time between visits exceeds the specified value the page requires it to again be downloaded from the host server. 
Redirect   This will redirect a user to another page.

 

Response.Write.  We've already used this method in our first ASP to write the output of our computations to the web page. It can be used with or without parentheses to delimit the expression that should be written to the web page.  You may need to make clever use of backslashes and quotes in order to write certain things to the web page.  

For example, since the %> is used as a delimiter for ASP code within a page, it is more difficult to use it within HTML that you want written.  The HTML command <HR WIDTH=50%> will cause a single horizontal line to be placed in a web page that is 50% the width of the page.  In order to make sure this is interpreted correctly you need to use a backslash just after the %.  You should try to implement both of the following ASPs.  One will give you an error, the other should do what we've just described.  Call the file PercentSign.asp.

 

<%@ Language=VBScript %>
<% Option Explicit %>

<HTML>
<BODY>
The current time is <%= Time() %>
<P>
<%
Response.Write("<HR WIDTH=50%>")
If DatePart("h",Time()) >= 12 Then
'Is after noon
    Response.Write "Good Not Morning!"
Else
'Is before noon
    Response.Write "Good Morning!"
End If
Response.Write("<HR WIDTH=50%>")
%>

</BODY>
</HTML>

 

Now you need to FTP it to your root web.  This will give you an error when you go to that ASP on your web.  What you need to do is modify the ASP slightly so that you put the appropriate backslashes into the code.  You should be able to copy the following code into your PercentSign.asp and then upload it.

 

<%@ Language=VBScript %>
<% Option Explicit %>

<HTML>
<BODY>
The current time is <%= Time() %>
<P>
<%
Response.Write("<HR WIDTH=50%\>")
If DatePart("h",Time()) >= 12 Then
'Is after noon

Response.Write "Good Not Morning!"

Else
'Is before noon

Response.Write "Good Morning!"

End If
Response.Write("<HR WIDTH=50%\>")
%>

</BODY>
</HTML>

 

This should run to give you the following page.  Notice that it doesn't actually print out the time to the web.  We'll fix this in a bit.

 

 

Similar things happen when you want things to appear in quotes since they are used as text delimiters.  In that instance you can use double quotes "" if you want a single " to appear on the resulting web page.  We won't work this example at present.

What would we need to do to get the current time to appear on our web page?  We need to use some sort of Response.Write.  There is another option that works well in situations where you only need to print out an isolated line of VBScript ASP code.  That is what we will cover next.

Using <%= . . . %>.  There is a shortcut notation for Response.Write that really is only effective when it is used on isolated lines of VBScript in an ASP.  We will discuss why that limitation is important in a little bit after developing our example.  Modify the PercentSign.asp code to be like the following and save it as TimeOfDay.asp.  Then you should upload it to your root web and go to the URL.

The code follows.

 

<%@ Language=VBScript %>
<% Option Explicit %>

<HTML>
<BODY>
The current time is <%= Time() %>
<P>
<%
Response.Write("<HR WIDTH=50%\>")
If DatePart("h",Time()) >= 12 Then
'Is after noon

Response.Write "Good Not Morning!"

Else
'Is before noon

Response.Write "Good Morning!"

End If
Response.Write("<HR WIDTH=50%\>")
%>

</BODY>
</HTML>

 

Notice that the only line that has changed has to do with  <%= Time() %> where the <%= has been put into the code.  This causes it to be written to the web page as if a Response.Write had been used.

The following image represents the new output of the ASP.

 

It should be fairly clear that you don't want to use this shortcut within the body of a larger group of VBScript because the termination character %> will tell the server that your encompassing code has ended before you want.