7.1.10- Using the Server Object to Control what the browser does
by NT Community Manager.
|
| << 7.1.9- Using the Response Object to Control what the Browser does | Chapter7 | 7.1.11- Other Information the Response Object Can Send >> |
Using the Server Object to Control what the browser does
As we've already intimated, Response.Redirect is now pretty much outdated with the introduction of two new methods of the Server object. We described the Server object in chapter 6 as being pretty much a miscellaneous grab-bag of methods and properties that loosely relate to the web server and that don't really fit in elsewhere. Whereas before you had to rely on the Response object to redirect you, providing you hadn't sent any text to the client, the redirection is now all done at a meta-Server level.
As with all redirection in any .asp file, if the executed .asp file attempts to modify HTTP headers after it sends a response to the client, it will generate an error. So, let's take a look at the two methods together. We're going to study them side by side because, apart from one small difference, they're effectively the same.
Server.Execute and Server.Transfer
The Execute method transfers execution across to a second page, executes that in its entirety and then returns control to the original page. It works as follows:
Server.Execute(Destination page)
The Transfer method transfers execution across to a second page, and executes that in its entirety, but does not return to the original page.
Server.Transfer(Destination page)
In both cases, the value of DestinationPage could be a string value, or hold a valid URL that the browser would then be told to retrieve. The destination page should be formatted just like a hyperlink would. If the URL is on the same site, then a relative reference can be used. Contrary to the IIS 5.0 online documentation, the Server.Execute and Server.Transfer methods can only be used locally so fully qualified http:// references are not supported, as documented in this Microsoft Knowledge Base article .
One thing to note about both of these methods is that for all intents and purposes you are working on the same page as before. So in other words, the URL in the browser won't change, any objects you created in the previous page will still work, in fact the scope of the session (something we'll look at in the next chapter ) will also be maintained.
Let's now take a look at an example which demonstrates the use of both methods. We'll use the methods to include the contents of the pages we created earlier, page one and page two.
Try It Out – Using Server.Transfer and Server.Execute
1. Open your HTML editor, create a new file, and type in the following:
<HTML>
<HEAD>
<TITLE>Transfer and Execute Example</TITLE>
</HEAD>
<BODY>
<%
Response.Write "We're here on the original page<HR>"
Server.Execute "page1.html"
Response.Write "<HR>We're back again on the original page<HR>"
Server.Transfer "page2.html"
Response.Write "<HR>We're back again on the original page"
%>
</BODY>
</HTML>
2. Save this as ExecuteTransfer.asp and display it on your browser:
|
|
Note that you will have to have worked through the previous example where you created two pages, page1.html and page2.html for this to work.
How It Works
The code is brutally straightforward. We start with Response.Write and finish with a horizontal line to indicate we're about to execute the first redirection:
Response.Write "We're here on the original page<HR>"
We do the first redirection, using Execute, which lets the Server know that we want to come back to this point once the next page has finished:
Server.Execute "page1.html"
What we are actually doing is executing the contents of this page (even though this page is only HTML, it would execute any ASP script contained within):
<HTML>
<HEAD>
<TITLE>Page 1</TITLE>
</HEAD>
<BODY>
<H1>This is Page Number 1</H1>
</BODY>
</HTML>
Once we have displayed this page, control is returned back to ExecuteTransfer.asp, at the point we left. So the next line displayed in this screenshot, displays another horizontal line, and a message indicating we are back on the original page.
Response.Write "<HR>We're back again on the original page<HR>"
Next, we redirect to our second page.
Server.Transfer "page2.html"
This time however, control isn't returned to the original page. So while page2.html is executed in this example, the final line is never executed:
Response.Write "<HR>We're back again on the original page"
Once page2.html has been displayed the code ends. However, take a look at the browser's URL line – there's not a hint that we have even left our original page.
So, hopefully you can see that Server.Transfer and Server.Execute offer you a greater degree of flexibility when redirecting to other pages, and that when you use them, you adhere to the same rules that govern sending text to the client on the output stream with the Response.Redirect method.
| << 7.1.9- Using the Response Object to Control what the Browser does | Chapter7 | 7.1.11- Other Information the Response Object Can Send >> |

RSS


