Developing Web Applications |
Sometimes, the page being requested by the browser isn’t the one you’d like to send. For example, suppose a user requests Oldpage.htm, which has been replaced by NewPage.asp. Standard HTML syntax provides a means whereby you can redirect (or divert) a request to another location. The syntax looks like this:
<HEAD><META HTTP-EQUIV="REFRESH" CONTENT="0;URL=NewPage.asp"></HEAD>
You can also use ASP to redirect a request to another page based on the logic of your application. The syntax is simple:
Response.Redirect "./NewPage.asp"
The Redirect method of the Response object operates by sending the “302 Object Moved” response header, plus the new location of the file, to the client. When it receives this response, the user’s browser automatically requests the new page.
Because redirection depends on HTTP headers, which come at the beginning of the document, you can’t redirect once text has been sent to the client. If you redirect in a server-side script after data has been sent to the client, the following error occurs:
Header Error
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
If you don’t know at the beginning of the page whether you need to redirect, you can use the buffering capabilities of the Response object. If Response.Buffer is True, HTML output is collected in a buffer and sent all at once to the client. If at some point you need to redirect to another page, ASP automatically discards any existing output in the buffer when you call Response.Redirect. You may also use Response.Clear at any time to clear the buffer and start again.
The following example demonstrates this concept:
<% 'Begin buffering the HTML.
Response.Buffer = True %>
<HTML>
<BODY>
HTML text before potential redirect.
<%
On Error Resume Next
'Script generates an error here.
If Err.Number > 0 Then
Response.Clear
Response.Redirect "./error.asp"
End If
%>
Once you call Response.Redirect, the script ends and the redirection headers are sent immediately. Any code following the redirect will not be executed, although it is required for syntactical correctness.
You can use Response.End to end a response from the server immediately and to send the current output to the browser. Since the response ends at that point, any HTML that follows is not sent. The following script detects that the user has connected anonymously (the LOGON_USER server variable is empty) and forces a logon by returning a “401 Access Denied” message.
<%
strLogon = Request.ServerVariables("LOGON_USER")
If IsEmpty(strLogon) Or strLogon = "" Then
'Up to this point, no HTML has actually been sent.
Response.Status = "401 Access Denied"
Response.End
End If
%>
<HTML>
You are logged on as: <%= strLogon %>
</HTML>
A well-behaved browser will try this page a second time, with logon credentials. If you don’t use Response.End, the script in the ASP page will execute twice, possibly leading to unexpected side effects (such as adding duplicate records to a database).
See the following: