Page

7.1.2- Sending Information back to the Client

Created by Brendan Doss.
Last Updated by Joel Bush.  

PublicCategorized as 07. The Request and Response Objects.

Not yet tagged
<< 7.1.11- Other Information the Response Object Can SendChapter77.1.3- The Request Object >>

Sending Information back to the Client

The primary function of the Response object is to send information to the client. Usually, this is a direct result of the client making a request of the server. It is the responsibility of the server-side script to come up with a response that will be understood by the client. Once this valid response information has been created, the Response object can be used to transmit the information.

 

There are two main ways that you can use the Response object to send text back to the client, which can be displayed on your web page. Both should be fairly familiar to you now – they are in fact two faces of the same beast. One way is by using the Write method of the Response object and the other way is a shortcut notation of Response.Write.

The Write Method

The Write method will be the most-used method of the Response object. The Write method is used to display text on your ASP page. It's a simple enough method, but when using it, there is a lot going on behind the scenes that we'll look at later in this chapter. For now we're going to take a simple overview, as it is very straightforward to use.

 

The Write method of the Response object allows you to display information on your web page. This information can be the contents of a server-side variable, the return value from a function, or a string constant. No matter what the information, the syntax of the method is consistent:

 

Response.Write value

 

The contents of value can contain any valid information that can be output in an HTML file. In other words, it is up to the developer to make sure that whatever data is contained in value can be properly displayed on a client browser. We'll be seeing many examples throughout this chapter, demonstrating how to use Response.Write.

Using Shortcuts

You can use a special 'shortcut' version of Response.Write to send information to the client. You can use shortcuts for outputting information to the client using a special form of the standard script delimiters. When adding ASP script to a HTML page, you use the <%….%> to indicate what is script and what is HTML. The shortcut method allows you to use a modified version of the script delimiter, <%=%> as a shortcut reference to Response.Write.
For example, to use the shortcut method to write the contents of the variable value as we did above, you would write:

 

<%= value %>

 

This would result in exactly the same information being written to the HTML output stream as we would get with this statement:

 

<% Response.Write value %>

 

Now, the question is, "When should I use which method?"

When to Use each Method

We have just seen two ways to output information to the client. There are some very simple rules that you can follow to help choose which of the two methods you should use.

 

The shortcut method is best used when you have a block of HTML code into which you want to insert a single piece of dynamic code. As you can see in these two examples that do the same thing, the shortcut method really reduces the amount of code that you have to write:

 

<BODY>

The time is now:

<%

Response.Write Now

%>

</BODY>

 

Now, if you were to rewrite this using the shortcut method, you can see the savings in code that you would gain:

 

<BODY>

The time is now: <%= Now %><P>

</BODY>

 

However, the shortcut method is not always the most efficient method in terms of performance. If you have a big block of ASP script code and want to output information to the client, then using the Response.Write method directly would be the way to go. The performance of a page will drop if you use the shortcut method because of jumping into and out of script, creating the resultant HTML bit by bit, rather than continuously in one script block. In a larger scale situation, your users will notice a drop in speed. In many situations, using the full notation also improves the readability of your code, which will make it easier to remember what you were doing when you look at the page two months later.

 

Here are a few quick notes about what you can and can't do with Response.Write

 

  • You can't have more then one line in the shortcut. The following is not acceptable

 

<%= "Hello world"

="Hello world%>

 

  • You can concatenate two strings in a Response.Write

 

strOne = "Hello"

strTwo = "World"

Response.Write strOne & strTwo

 

  • You can calculate the result of a mathematical expression

 

intInteger1 = 26

intInteger2 = 456

Response.Write intInteger2/intInteger1

 

  • You can have a value which is from a Request object

 

Response.Write (Request.QueryString("Query1"))

 

Now let's take a look at an example.

Try It Out – Using Response.Write and the Shortcut method

In this example, we will be looking at the two ways that you can send dynamic information back to the client by using the two methods of the Response object.

 

1.    Using your editor of choice, create the OutputTest.asp file with the following source code:

<HTML>

<HEAD>

<TITLE>Testing the Write Method</TITLE>

</HEAD>

<BODY>

Here is some plain HTML being added to the HTML output stream<BR><BR>

<%

Response.Write "Here is a string being output using Response.Write<BR><BR>"

%>

<%= "Here is a string being output using the shortcut method<BR><BR>" %>

<HR>Now let's try some dynamic text<BR><BR>

<%

Response.Write "With Response.Write, the time is now: "

Response.Write Now

Response.Write "<BR><BR>"

%>

With the shortcut method, the time is now: <%= Now %><BR>

</BODY>

</HTML>

 

2.    Save the file to your usual BegASP directory.

3.    View the page in your web browser.

 

Chapter7_image002

How It Works

The use of these two methods is fairly self-explanatory. Basically, whatever information you pass as the parameter to the method will be sent back to the client for display. This information can be a string constant, a variable, or the value of a built-in function. Anything goes, as long as the output is something that the browser will understand.

 

You can also see in this example that while you can use both Response.Write in full and the shortcut method to output information to the client, the context in which you output the information should determine what method you would use. Given a choice like this, most developers will choose the method that is the easiest to read and that also requires the fewest keystrokes. None of us likes typing, but it goes along with the job!

<< 7.1.11- Other Information the Response Object Can SendChapter77.1.3- The Request Object >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |