Sending output to the user's browser

There are two ways to send data to the user (browser). First, we can use the Response object to not only send output to the user but also to control the output in some fashion. Secondly, we can use the shorthand (called delimiters) of the Response object to send data to the user. Let's work some examples to show the differences between these two methods and their use. The following prints a simple statement using the response object:

<% response.write "Sending a line to the browser!" %>

We can convert this ASP response statement to our method of sending output to the client:

<% = "Sending a line to the browser!" %>

The output of both of these statements is the same:

Sending a line to the browser!

Both of these methods of printing output include the <% and %>, called delimiters. These delimiters mark the beginning and end, respectively, of your ASP code. Because in the first example we are using the response object, it seems it has more code than the second example. One of the main reasons to use the second method for sending output to the user is that it is short (it saves you some typing).

Unfortunately, this method has some limits. First of all, the your output cannot wrap onto more than one line. If you write a long line possibly consisting of HTML formatting instructions using this method, it will be difficult for you to edit or understand it. Secondly, it can create performance problems if you use a lot of these shorthand delimiters for printing. Because each time you begin and end your ASP code, the ASP engine has to process that code. Calling the ASP engine unnecessarily may result in performance issues. Therefore, it is probably best to avoid use of this method for printing if you have more than one line to print per ASP file.

The better method for printing output is the response object with the write method. A method is an action that is part of an object. In this case, the action of the write method is printing output to the screen.

In the following example, we embed HTML syntax into our ASP code:

<% = "<strong>Sending</strong> a line to the browser!" %>

In this simple example, we emphasize the word "Sending" by making it bold. We could even output an entire HTML page in ASP. This is useful when we want to create dynamic pages on the fly. But, for simplicity, we will limit our examples to creating variety of outputs to cover the different techniques used in sending output to the browser. The following example shows how to print a complete HTML table output with ASP:

<% = "<table><tr><td>heading 1</td><td>heading 2</td><td>heading 3</td></tr><tr><td>row 1, cell 1</td><td>row 1, cell 2</td><td>row 1, cell 3</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td><td>row 2, cell 3</td></tr></table>" %>

The above shown code must be placed as one line for it to work correctly as we are not using the complete response object with the write method. The following shows how to create this table using the response object with the write method (notice how organized this code is with just addition of proper spacing!):

response.write "<table>"
response.write "<tr>"
response.write "<td>heading 1</td>"
response.write "<td>heading 2</td>"
response.write "<td>heading 3</td>"
response.write "</tr>"
response.write "<tr>"
response.write "<td>row 1, cell 1</td>"
response.write "<td>row 1, cell 2</td>"
response.write "<td>row 1, cell 3</td>"
response.write "</tr>"
response.write "<tr>"
response.write "<td>row 2, cell 1</td>"
response.write "<td>row 2, cell 2</td>"
response.write "<td>row 2, cell 3</td>"
response.write "</tr>"
response.write "</table>"

We could also avoid typing of response.write by assigning the output to a variable:

dim shtml
shtml = "<table>"
shtml = shtml & "<tr>"
shtml = shtml & "<td>heading 1</td>"
shtml = shtml & "<td>heading 2</td>"
shtml = shtml & "<td>heading 3</td>"
shtml = shtml & "</tr>"
shtml = shtml & "<tr>"
shtml = shtml & "<td>row 1, cell 1</td>"
shtml = shtml & "<td>row 1, cell 2</td>"
shtml = shtml & "<td>row 1, cell 3</td>"
shtml = shtml & "</tr>"
shtml = shtml & "<tr>"
shtml = shtml & "<td>row 2, cell 1</td>"
shtml = shtml & "<td>row 2, cell 2</td>"
shtml = shtml & "<td>row 2, cell 3</td>"
shtml = shtml & "</tr>"
shtml = shtml & "</table>"
response.write shtml

The ampersand (&) character above enables us to combine text together. For instance, first we assign the text "<table>" to the variable named shtml. On the next line, to the same variable we add more text, namely, "<tr>." We continue to add more text to the same variable until we have not finished all of our HTML code for creating a table. Combining of strings in this fashion is string concatenation. This is one of the commonly used tricks to produce long output.

Finally, here is the output of our code:

heading 1heading 2heading 3
row 1, cell 1row 1, cell 2row 1, cell 3
row 2, cell 1row 2, cell 2row 2, cell 3

Because we are surrounding our text with double-quote characters, it will be a great challenge for you to figure out how to send double-quote characters to the browser from your ASP code. This is especially true if you are new to ASP. Also recall that properly formatted HTML document have attribute values surrounded by double-quote characters. Thus when you use HTML attributes in ASP code, you will need to surround them with double-quote characters as well. For example, suppose you wanted to add attributes to your HTML tag in ASP. Assume this is our HTML code:

<table width="200" border="0" cellspacing="0" cellpadding="2">

The equivlalent of sending that HTML code to the browser from an ASP generated script is:

<% response.write "<table width=""200"" border=""0"" cellspacing=""0"" cellpadding=""2"">" %>