Response.Redirect in Classic ASP

Response Object and HTML Encoding

The response object is often used in conjunction with various kinds of coding schemes. No discussion of response would be complete without a discussion of how to “handle” or “escape” special characters. This sample script demonstrates common conversion and transformation commands that make sense to use with the response.write command:

<html><head>
<title>Response object</title>
</head><body bgcolor="#FFFFFF">
<%
' The response object can be used to write text
' but sometimes some functions must be used to transform
' the text instead of sending as is to the browser

response.write "<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel"
response.write "<p>"
response.write server.htmlencode("<B>Hyperion</b> by <I>Dan Simmons</i> is a great novel")
response.write "<p>"

response.write "Joe Smith & Hilda = a team"
response.write "<p>"
response.write server.URLencode("Joe Smith & Hilda = a team")
%>

</body></html>

Response Object – Redirects

The response object can be used to decide what page to send a user to next. Specifically the response.redirect method will work in that capacity. We have made a script formjump.asp that takes advantage of this.

<html><head>
<TITLE>FormJump.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form action="FormJumpRespond.asp" method="get">
<SELECT NAME="wheretogo">
<OPTION SELECTED VALUE="fun">Fun</OPTION>
<OPTION value="news">Daily News</OPTION>
<OPTION value="docs">ASP IIS3 Roadmap/Docs</OPTION>
<OPTION value="main">MainPage of ActiveServerPages.com</OPTION>
<OPTION value="sample">IIS 3 Sample ASP scripts</OPTION>
</SELECT>
<input type=submit value="Choose Destination">
</form>
</body></html>
The responder that reacts to this form is:

<%response.buffer=true%>
<html><head>
<title>formjumprespond.asp</title>&
<body bgcolor="#FFFFFF">
<%
' My ASP program that redirects to URL
thisURL="http://www.activeserverpages.com"
where=Request.QueryString("Wheretogo")
Select Case where
case "main"
response.redirect thisURL & "/"
case "samples"
response.redirect thisURL & "/aspsamp/samples/samples.htm"
case "docs"
response.redirect thisURL & "/iasdocs/aspdocs/roadmap.asp"
case "news"
response.redirect "http://www.cnn.com"
case "fun"
response.redirect "http://www.dilbert.com"
End Select
response.write "All dressed up and I don't know where to go<br>"
response.write "I recommend --> " & "<br>"
response.write server.htmlencode(thisURL & "/learn/test/res2.asp?where=fun") & "<br>"
response.write "for a good laugh!" & "<P>"
%>
</body></html>
Advertisements

Do..While..Loops in ASP

Do Loop Part #1  by Charles Carroll

To execute a code sequence more than once ASP provides:
* DO, LOOP
* WHILE, WEND
Either of these statements can be followed by UNTIL or WHILE.

DO UNTIL
…..code to be repeated…
LOOP

DO
…..code to be repeated…
LOOP UNTIL

Do Loop and Timeouts by Charles Carroll

A loop that is infinite will not run forever. IIS will timeout the script (default is 90 seconds).

Here is an infinite loop that IIS will timeout:

<%response.buffer=true%>
<TITLE>doloop1.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
response.write counter & "<br>"
response.flush
LOOP
%>
</BODY>
</HTML>

Here is an infinite loop that we explicitly set a timeout for:

<%
response.buffer=true
server.scripttimeout=20
%>
<TITLE>loop2.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
response.write counter & "<br>"
response.flush
LOOP
%>
</BODY>
</HTML>

It has been assumed that a timed out script was impossible to intercept, but the next lesson shows how to use the transactional aspect of an ASP script to capture this elusive condition.

Do Loop Intercept Timeouts by Charles Carroll

The transactional nature of ASP pages can be used to intercept a script timeout.
loop3.asp traps a timeout:

<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=20
%>
<HTML>
<TITLE>loop3.asp</TITLE>
<body bgcolor="#FFFFFF">
</BODY>
<%
DO
counter=counter+1
response.write counter & "<br>"
LOOP
response.flush
response.write "Script executed without incident"
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
Response.Write "The Script Timed Out"
end sub
%>

loop4.asp succeeds and does not trigger the trap:

<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=40
%>
<HTML>
<TITLE>loop4.asp</TITLE>
<body bgcolor="#FFFFFF">
</BODY>
<%
DO  UNTIL counter=400
counter=counter+1
response.write counter & "<br>"
LOOP
response.flush
response.write "Script Exexuted without incident!"
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
Response.Write "The Script Timed Out"
end sub
%>