7.1.9- Using the Response Object to Control what the Browser does
by NT Community Manager.
|
| << 7.1.8- Controlling How Information is Returned | Chapter7 | 7.1.10- Using the Server Object to Control what the browser does >> |
Using the Response Object to Control what the Browser does
In addition to sending HTML code back to the client, the Response object can be used to control some of the things that the browser itself does.
- If you want the browser to cache the page that you are creating until a certain date, or for a certain length of time, you can use the Response object to pass the browser this information.
- If the browser requested a certain page, but you really think that it should be displaying another page, you can use the Response object to tell the browser to go get another page. This could be used if you have moved a page in your site, but still want people with links to the original page to be able to get to the information. This technique is now becoming outdated with the introduction of the Transfer and Execute methods for the Server object in ASP 3.0 (which we'll look at shortly), although there are feasible occasions when you might still wish to use it.
The use of these methods is a bit more complex than just sending information back to the browser. They provide us with increased flexibility, and are worth discussing a little further here.
Content Expiration and Caching
As you are surfing the web using your web browser, you are downloading pages and images and other types of content to your computer so that the browser can display them. Since most people tend to visit the same sites regularly, browsers have been developed with a cache that exists on the client's computer. A cache is a temporary storage area, like a buffer, but while a buffer is effectively a queuing mechanism onto which information is placed in order, a cache retains no order and is used to retain information for a lot longer. You can see the cache for Internet Explorer on Windows 98/2000 in
C:\Windows\Temporary Internet Files (Netscape uses a different area, along the lines of
C:\Program Files\Netscape\Users\Default\Cache).
The browser will fill this cache up with the information that is downloaded from various web sites. Later, if the user returns to a site they had already been to, the browser can check to see if the page that the user is requesting is in its cache. If it is, then the page can be displayed immediately, rather than having to wait for it to download from the server. Web site developers can use this cache to their advantage as well. HTML pages are not the only items that are cached. All JPG and GIF images are also cached. Knowing this, web site developers can reuse graphics in their site, so that the only time they have to be downloaded is for the first page that uses them.
When the browser is asked to display a page that it already has in cache, it will check that cached page to see if it has expired. A web page developer can set the expiration date of a page so that they can ensure that the person viewing the page is always seeing the latest content, but not forcing them to download the page every time.
As an ASP developer, we have a bit of a dilemma. Our pages are dynamically created, so they are, in effect, different pages every time. Even if the underlying data that generated the page hasn't changed, a new page is always freshly created to send to the browser. There are many cases where the page will only change on a periodic basis. We would like the clients viewing the page to be able to take advantage of the caching that their browser offers. There are two properties of the Response object that we can use to control the expiration of the page that is generated. These properties are Expires and ExpiresAbsolute
Expires
The Expires property specifies the number of minutes before a page cached on a browser expires. If the user returns to the same page before the specified number of minutes have elapsed, the cached version is displayed.
To set this property, you would use:
<% Response.Expires = minutes %>
In this example, minutes represent the number of minutes before this page will expire. Setting minutes to 0 causes the page to be refreshed with each access. Setting the page to –1442 might be better if you're expecting users from anywhere in the world to browse the page, since all time zones are covered this way (60 min/hr times 24 hours plus two to cover rounding at both ends.)
ExpiresAbsolute
The ExpiresAbsolute property sets the date and/or time at which a page cached on a browser expires. If the user returns to the page before the specified date and/or time, the cached version is displayed. If no time is set, the page expires at midnight of that day. If a time is set, the page expires at the specified time on the day that the script is set to expire.
To set this property, you would use:
<% Response.ExpiresAbsolute = #DateTime# %>
The value of DateTime must be a valid date or time combination. The value must be enclosed within the # signs. If you set this value to a date in the past, this is the same as setting the Expires property to 0. Again, if you're expecting users from anywhere in the world, it's best to set this value to two days into the past to cover all times zones.
Note that both of these properties set HTTP response headers to achieve their task. Therefore, they both need setting before any text is output to the client.
Redirection
When a client requests a specific ASP page from the server, the script for that page is processed. As that page is being processed, we may determine that there is a different ASP page that the browser should actually be displaying. For example, we could have a page that validates a user's login. Based on the results of that validation, the user could be shown a guest page or a registered user's page.
We could have the code to create both pages within the same script, but that would make for a very complicated script file. It would also make it more difficult for another developer to look at the page at a later time, and understand what we were doing. It would be much easier if we could check the user's login and, based on what the results were, send the user to a completely different page.
The old way of doing this was using the Redirect method of the Response object. This method would tell the browser that it needed to go and fetch a different page. This page could be on the same site, or it could be on a completely different web site. There are now two newer, more flexible methods of the Server object, Server.Transfer and Server.Execute, which can also do the same task. We'll look at these in the next section. However, the Redirect method isn't totally obsolete, and one important difference is that you can't send a querystring in the Server.Transfer or Server.Execute methods, as you can with the Redirect method. This is because the Redirect method tells the browser to send another request, something the Server object methods can't do.
Another difference is that while Transfer and Execute hide the fact that you've been moved to another page, the Redirect method displays the new page address in its URL. Also, you may be have to debug or update a page that contains this method, so it helps to understand it. Consequently, we will consider all three methods, as there maybe times when you want redirection to be transparent.
To call the Redirect method, you would use:
<% Response.Redirect "destinationPage" %>
The value of DestinationPage would need to be a string value, and hold a valid URL that the browser would then be told to retrieve. The destination page should be formatted just as a hyperlink would. If the URL is on the same site, then a relative reference can be used. If it is on a different site, then you need to include a full http:// reference.
There are a few things that you need to be careful with when using the Redirect method. A browser is told to fetch a different page through the use of an HTTP header. The problem lies in the fact that we cannot modify the HTTP status and add a header to the response after the body has gone. So, if the ASP script has output the HTTP headers, or has added any information to the HTML output stream, then calling the Redirect method will cause an error.
Earlier in this chapter, we learned how to control the contents of the HTML output stream by using the Buffer property. Setting this property to TRUE goes hand in hand with using the Redirect method. By buffering the HTML output, we can call Redirect almost anywhere, as long as we clear out the buffer by calling the Clear method before redirecting the browser.
Remember, if you are going to use the Redirect method, be sure to set the Buffer property to True at the beginning of the page.
Try It Out – Redirecting the Browser
In this example, we will show how you can create a page that processes some information from the Request object and makes a decision on which page (page number 1 or page number 2) the browser should display. Once it has made that decision, it will use the Redirect method to tell the browser to load that page.
1. Using your favored editor, create the PageChoice.html file with the following source code and save it in your BegASP directory:
<HTML>
<HEAD>
<TITLE>Redirection Example</TITLE>
</HEAD>
<BODY>
<H2>Choose which page you wish to display</H2>
<FORM ACTION="choosePage.asp" METHOD="POST">
<INPUT TYPE="Radio" NAME="PageChoice" VALUE="Page1" CHECKED>Page Number 1<BR>
<INPUT TYPE="Radio" NAME="PageChoice" VALUE="Page2">Page Number 2<P>
<INPUT TYPE="Submit" VALUE="Choose Page"> <INPUT TYPE="RESET">
</FORM>
</BODY>
</HTML>
2. Create another file and save it as choosePage.asp in the BegASP directory. This file should contain the following code:
<%
Option Explicit
Dim strChoice
strChoice = Request.Form("PageChoice")
If strChoice = "Page1" Then
Response.Redirect "page1.html"
Else
Response.Redirect "page2.html"
End If
%>
Remember that for this to work, Buffer needs to be set to True. We're not setting it true explicitly here, because it is already set to true by default.
3. Next, create and save a file called Page1.html with the following source code:
<HTML>
<HEAD>
<TITLE>Page 1</TITLE>
</HEAD>
<BODY>
<H1>This is Page Number 1</H1>
</BODY>
</HTML>
4. Create the Page2.html file with the following source code.
<HTML>
<HEAD>
<TITLE>Page 2</TITLE>
</HEAD>
<BODY>
<H1>This is Page Number 2</H1>
</BODY>
</HTML>
5. Make sure all these files are stored in you BegASPFiles directory
6. View the PageChoice.html file via your web browser:
|
7. Select Page Number 1 and press the Choose Page button. This should produce:
|
8. Go back to the PageChoice.html page and choose the other option.
|
How It Works
The first page that we are creating is a straightforward static HTML page. The job of this page is to present a standard HTML form that will allow the user to select which page they wish to display.
<FORM ACTION="ChoosePage.asp" METHOD="POST">
<INPUT TYPE="Radio" NAME="PageChoice" VALUE="Page1" CHECKED>Page Number 1<BR>
<INPUT TYPE="Radio" NAME="PageChoice" VALUE="Page2">Page Number 2<P>
<INPUT TYPE="Submit" VALUE="Choose Page"> <INPUT TYPE="RESET">
</FORM>
The form displays two Radio buttons, which allow the user to select which page to display. The results of the form are going to be sent to the choosePage.asp file for processing.
<%
Option Explicit
Dim strChoice
strChoice = Request.Form("PageChoice")
The first thing we want to do is add the Option Explicit statement to help ensure that we build up the good habit of always declaring the variables that we will be using in advance. The next thing we want to do, when processing the form, is to grab the selection that the user made in the previous page.
Once we have declared the variable to hold the user's selection, we will retrieve it from the Form collection of the Request object. In our HTML form, we had two radio buttons with the same name, PageChoice, but with different values. Depending on which button the user selected before submitting the form, the value for that button will be added to the Form collection.
If strChoice = "Page1" Then
Response.Redirect "page1.html"
Else
Response.Redirect "page2.html"
End If
%>
We can then compare the value that was passed from the form with the possible values. Based on that comparison, we can choose to send the browser to either page1.html or to page2.html. You will notice that there are no Response.Write statements, or any HTML text outside of the <% ... %> block. This ASP file is designed to produce no output to send to the browser: it is simply designed to evaluate some data that is passed to it, and based on that evaluation, to send the browser that is making the request to another page. In this case, both of the possible destination pages are static HTML files. They could have just as easily been ASP scripts, or even pages on a completely different web server. If that were the case, then we would need to use the complete URL as the parameter to the Redirect method. For example:
Response.Redirect "http://www.wrox.co.uk"
The critical thing to remember from this example is that when you are using the Redirect method, make sure that you are not sending any information to the HTML output stream. If you are, then make sure that you turn buffering on, and clear the output stream before calling redirect.
Amending our Program
Of course, radio buttons aren't the only way to send information; you could as easily use a drop down list box for example to display the same selection of options. (These are covered in Appendix F.) In this case, all you would need to is open up PageChoice.html and make the following adjustments.
<H2>Choose which page you wish to display</H2>
<FORM ACTION="choosePage.asp" METHOD="POST">
<SELECT NAME="PageChoice">
<OPTION VALUE="Page1">Page 1
<OPTION VALUE="Page2">Page 2
</SELECT>
<BR>
<BR>
<INPUT TYPE="Submit" VALUE="Choose Page"> <INPUT TYPE="RESET">
</FORM>
None of the other programs need altering, as choosepage.asp simply works on a value passed from an 'entity' called PageChoice, whether it's a radio button or a drop down list box. The same principles apply for different types of list box or check box.
| << 7.1.8- Controlling How Information is Returned | Chapter7 | 7.1.10- Using the Server Object to Control what the browser does >> |

RSS




