asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

The New ASP 3.0 Server Methods
By Paul Litwin
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Active Server Pages 3.0 offers many improvements and changes, most dealing with reliability, performance, and scalability, but Microsoft also added two new methods to the Server object: Transfer and Execute. (This version of ASP is part of IIS 5.0, which ships with Windows 2000.)

    Server.Transfer

    ASP developers have used Response.Redirect since the days of ASP 1.0 to redirect the browser to another page. Perhaps, like me, you thought this was all occurring on the server and was thus a fairly efficient operation, but that's not how it works. When the ASP engine encounters a Response.Redirect method, it stops the processing of the current page and sends an HTTP redirection header (302 Redirect) to the client, informing it that the page it requested has moved and can be found at a different URL. When this response is sent to the browser, the browser requests the new URL and the ASP engine sends the new page to the client. Thus, the redirection of a page using Response.Redirect requires an extra client/server round trip.

    Server.Transfer works differently than Response.Redirect. When the ASP engine encounters a Server.Transfer method on the page, it stops processing the page and sends the new page to the client in the response. That is, Server.Transfer substitutes the request for one page with another without involving an extra round trip between the server and the browser. In fact, as far as the browser is concerned, it received the page it originally requested. Let's illustrate with an example. Say you wished to branch in your ASP code and send users to one page or another based on whether they were using Internet Explorer or some other browser. The following code from Redirect1.asp accomplishes this employing the Browser Capabilities component and the Response.Redirect method:

    Here's the code behind Redirect.1.asp:

    
    <%@ Language=VBScript %>
    <%Option Explicit%>
    <% 
    Dim brws, strBrowser
    
    Set brws = Server.CreateObject("MSWC.BrowserType")
    strBrowser = brws.browser
    Set brws = Nothing
    
    If strBrowser="IE" Then
    	Response.Redirect("TargetIE.asp")
    Else
    	Response.Redirect("TargetOther.asp")
    End If
    %>
    
    
    If you attempt to navigate to Redirect1.asp using Internet Explorer, you should end up with a page that looks like the one shown in Figure 1.


    Figure 1. Redirect1.asp redirected the user to this page using the Response.Redirect method.

    Notice that the URL of the page is the URL of the redirected page, not the original page. Transfer1.asp achieves the same result as Redirect1.asp using Server.Transfer:

    
    <%@ Language=VBScript %>
    <%Option Explicit%>
    <% 
    Dim brws, strBrowser
    
    Set brws = Server.CreateObject("MSWC.BrowserType")
    strBrowser = brws.browser
    Set brws = Nothing
    
    If strBrowser="IE" Then
    	Server.Transfer("TargetIE.asp")
    Else
    	Server.Transfer("TargetOther.asp")
    End If
    %>
    
    
    When you attempt to navigate to Transfer1.asp using Internet Explorer, you end up with a page that looks like the one shown in Figure 2.


    Figure 2. Transfer1.asp redirected the user to this page using the Server.Transfer method.

    Because the redirection was performed entirely on the server, without any extra round trip between server and browser, as far as the browser is concerned it has received the Transfer1.asp page. You can verify this by looking at the URL shown in the Address box.

    Not All Good

    Server.Transfer has several advantages over Response.Redirect:

    • Because it saves a round trip between the server and the browser it's faster and reduces the load on the Web server.
    • The Response querystring and form collections are preserved during the transfer. As a result, you don't need to worry about reposting form and querystring data to the new page.
    However, Server.Transfer does have a few disadvantages:
    • You can only use Server.Transfer to redirect to a page on the same Web server.
    • You can't pass a querystring to the new page. (However, remember that the querystring passed to the page executing the transfer will automatically be passed along to the new page.) If you try to pass a querystring to the new page, you will trigger an ASP error.
    • The browser is never notified of the new page, which can cause problems with relative links in some cases.
    I've found that this last point can be a show-stopper for using Server.Transfer. If you use Server.Transfer to transfer execution to page in a different folder, all of your relative links will break. This includes any relative links to images, anchor tags, or style sheets on the page. For example, here's the source code for Transfer2.asp:
    
    <%@ Language=VBScript %>
    <%Option Explicit%>
    <% 
    Dim brws, strBrowser
    
    Set brws = Server.CreateObject("MSWC.BrowserType")
    strBrowser = brws.browser
    Set brws = Nothing
    
    If strBrowser="IE" Then
    	Server.Transfer("SubFolder/TargetIE2.asp")
    Else
    	Server.Transfer("SubFolder/TargetOther2.asp")
    End If
    %>
    
    
    Notice that this page is identical to the Transfer1.asp with one exception: the target pages are located in a subfolder (appropriately named "SubFolder") of the Web site. This time, when you attempt to navigate to Transfer2.asp using Internet Explorer, you end up with a page that looks like the one shown in Figure 3.


    Figure 3. The links to the image and hyperlink on this page are broken because we used Server.Transfer to transfer to a page in a different folder.

    Notice that the link to the image is broken. In addition, if you click on the hyperlink on the page you will get a "Page Not Found" message. This happens because the browser thinks that the relative links on the page are relative to the location of Transfer2.asp, not the location of SubFolder/TargetIE2.asp. In contrast, Redirect2.asp page, which uses Response.Redirect instead of Server.Transfer, doesn't have any problem redirecting to a page in a different folder as is shown in Figure 4.


    Figure 4. The links on this page work fine even though the page is located in a different folder than the original page because we used Response.Redirect instead of Server.Transfer.

    If you need to redirect users to a page in a different folder, you should use Response.Redirect.

    Server.Execute

    Another new Server method introduced by ASP 3.0 is Server.Execute. Use Server.Execute to run a chunk of ASP code on another page, returning to the original page when the code is done executing. In many ways, Server.Execute serves a similar purpose to server-side includes. However, because Server.Execute is an ASP method rather than an HTML comment, you can use it to conditionally execute scripts and avoid including huge include files. Server-side includes are executed prior to any ASP code and thus can't be executed conditionally.

    Let's illustrate how Server.Execute works with an example. The following hyperlinks appear on the Customer1.asp page:

    
    <A HREF="customer2.asp?type=old">Existing Customers Click Here</A>
    </P>
    <P>
    <A HREF="customer2.asp?type=new">New Customers Click Here</A>
    
    
    Both hyperlinks on Customer1.asp point to a second page, Customer2.asp, passing it a querystring value of type=old or type=new. Customer2.asp contains the following code, which conditionally executes code on NewCust.asp or OldCust.asp based on the value of the "type" query string:
    
    <%@ Language=VBScript %>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    
    <%
    If Request.QueryString("type")="new" Then
    	Server.Execute("NewCust.asp")
    Else
    	Server.Execute("OldCust.asp")
    End If
    
    Response.Write("<P><BIG>Whether you are a new or existing " & _
     "customer, we are certain that you will appreciate the attention " & _
     "that Conglomerated Conglomerates gives to you, our esteemed " & _
     "customer and partner.</BIG></P>")
    %>
    </BODY>
    </HTML>
    
    
    In this trivial example, the code in NewCust.asp and OldCust.asp merely executes a few Response.Writes, but you can execute any ASP code. What's so nice about Server.Execute is that the ASP engine parses the code in the executed page only if the Server.Execute statement is executed.

    One drawback to Server.Execute is that it can't call a particular procedure on the included page. Server.Execute starts running code at the top of the executed page and continues running the code until the executed page ends, at which point it returns to the original page. Another potential drawback with Server.Execute is that any page-scope variables are not shared between the original page and the executed page. With server-side includes this isn't an issue because the include file code is incorporated into the original page before it is executed. Regardless, there are many situations where using Server.Execute is a better choice than using server-side includes.

    Summary

    In this article, you learned about the new ASP 3.0 Server methods: Transfer and Execute. In many cases, you can improve the performance of your Web site by using these new methods. However, as I hope I have demonstrated in this article, there at least some situations where you are better off using Response.Redirect rather than Server.Transfer and server-side includes instead of Server.Execute.

    About The Author

    Paul Litwin is the CEO of Litwin Consulting. He is the author of numerous articles and several books, including the Access 2000 Developer's Handbook. Paul is currently working on ASP.NET for Developers with coauthor Mike Amundsen. He travels around the U.S. training developers and has authored courseware for AppDev's Active Server Pages and Visual InterDev, and created Access classes and CD and video seminars. Paul is a regular speaker at industry conferences and is the Conference Chair for the ASP Connections conference (www.asp-connections.com). You can reach Paul at plitwin@mcwtech.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    XCache
    XCache combines dynamic content caching technology with content delivery network (CDN) support options, file compression and a whole lot of manageability features to help e-businesses deliver superior web site performance and reliability. You'll appreciate the administrative ease, your visitors will appreciate increased page delivery speed.
    [Top]
    XCompress
    XCompress works by compressing outgoing text between the Web server and the client. Page response times may improve by a factor of three or more while overall bandwidth use can drop by two thirds or more.

    XCompress runs on Windows 2000 and Windows NT 4.0 and is tightly integrated with Microsoft Internet Information Server (IIS) with MMC and COM interfaces.

    [Top]
    XTune
    XTune 2.0 is the most powerful tuning application for IIS 4 or IIS 5 ever conceived. Indispensable to the enterprise and straightforward, this web tuning tool allows you to configure hidden operating system, network, Active Server Pages and Internet Information Server settings for better performance, without any additional hardware or software.

    This version scans your system more deeply, offering more performance-enhancing recommendations and greater insight into your web architecture. The Performance Wizard guides and teaches you throughout the complete tuning process, so you can learn while making your box run better than ever.

    Purchase here.

    [Top]
    Other Articles
    Aug 25, 2005 - Performance Monitoring in SharePoint Portal Server 2003
    Performance monitoring helps organizations identify performance bottlenecks. The problem is that with so many performance numbers available, how do you know which ones to watch? This article helps you identify which are the critical performance counters in a SharePoint Portal Server environment and explains how to monitor them. By monitoring performance regularly, organizations can recognize performance trends as they develop and prevent problems before they get out of hand.
    [Read This Article]  [Top]
    Aug 12, 2004 - Middle-Tier Hosting: Enterprise Services, IIS, DCOM, Web Services, and Remoting
    There is broad-reaching debate about remoting, Web services, Enterprise Services, and DCOM. In short, it is a debate about the best technology to use when implementing client/server communication in .NET. Rocky Lhotka shares his thoughts on the issue while offering clear explanations of basic application architecture terminology.
    [Read This Article]  [Top]
    May 18, 2004 - ASP.NET 2.0 Caching Features
    This article examines some of the new and exciting caching features in ASP.NET 2.0 and shows how to implement them in Web applications.
    [Read This Article]  [Top]
    Feb 12, 2004 - Case Study: Match.com
    When it came time to find a technology for its massive upgrade, Match.com chose .NET. Has the online dating service's partnership with Microsoft been as successful as the relationships it has established for many of its millions of members? Read on ...
    [Read This Article]  [Top]
    Jan 15, 2004 - Database Performance Philosophy
    Longtime 15Seconds discussion list member Tore Bostrup offers valuable advice on designing databases and applications for efficient querying.
    [Read This Article]  [Top]
    Dec 29, 2003 - Caching Oracle Data for ASP.NET Applications
    Narayan Veeramani shows how ASP.NET developers can improve application performance by caching data stored in an Oracle database and keeping the cached data in sync with the data in the Oracle database.
    [Read This Article]  [Top]
    Dec 2, 2003 - Leveraging MSMQ in ASP.NET Applications
    Ever developed a Web application that requires extensive processing? Ever had long running Web pages that often time out in the browser? Greg Huber reveals a simple technique that uses Microsoft Message Queuing (MSMQ) and the System.Messaging framework to handle long running Web processes.
    [Read This Article]  [Top]
    Mar 14, 2002 - Web Site Compression
    As IT professionals try to reduce the cost of operating their Web sites, they should consider reducing the amount of bandwidth usage. Learn how to successfully compress your HTML output and save money on your monthly bandwidth.
    [Read This Article]  [Top]
    Feb 6, 2002 - The Just Two Theory on Web Servers
    Maintaining a large Web farm is both costly and unnecessary. Learn how to reduce your Web farm to just two servers in this controversial article by Wayne Berry.
    [Read This Article]  [Top]
    Aug 14, 2001 - NT Authentication's Impact on Connection Pooling
    Steve Witkop examines OLE DB and ODBC connection pooling when used with Microsoft NT LAN Manager Web server authentication.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers