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
%>

Working with Date and Time in ASP

Summary

Including the date and/or time on a web page can be a subtle yet valuable addition when designing a web site. The addition of the date to the home page can create the impression that a site is constantly being updated with new content since each time a visitor loads the page, the current date will be displayed.

In this tutorial I’ll teach you how to add the date and time to your ASP pages using the VBScript FormatDateTime() function. I’ll explain how the function works, teach you how to integrate it into your ASP pages and illustrate the output you’ll get depending on the arguments you pass. I’ll round out the tutorial of the FormatDateTime() function by covering a few limitations that it has, which might or might not be a big deal depending on your specific needs.

Just FYI, this article assumes you know basic HTML and how to add ASP scripts to your web pages.

The FormatDateTime() Function

Microsoft provides a ton of predefined VBScript functions designed to reduce coding time. The FormatDateTime() function is one of those powerful functions and is really easy to use, too. This function uses the following format:

FormatDateTime(date, format)

There are two arguments the function accepts: date and format. Table 1-1 below describes these arguments in greater detail:

Table 1-1: The FormatDateTime() function and its arguments

Argument Argument Description
date This argument is required and can be any valid date expression such as Date or Now

format This format constant or format value specifies how the date and/or time will be displayed on your ASP page.

When specifying the format argument, you can either type the Visual Basic constant name (name in left column), or the constant’s corresponding value (0 – 4, from the middle column). They do the same thing, it’s just less typing if you use the value.

Constant
Format Value
Format Description
vbGeneralDate
0
This is the default. Not specifying a value or specifying 0 will produce a date in the format of mm/dd/yy.

If the date expression is Now, it will also return the time, after the date, in hh:mm:ss PM/AM format.

vbLongDate
1
This is my personal favorite 🙂 Passing this value will produce a date in the format of
weekday, month day, year*

* The year is Y2K compliant :-).

vbShortDate
2
Passing this value returns a date formatted just like the default of 0 (mm/dd/yy).
vbLongTime
3
Passing this value returns the time in hh:mm:ss PM/AM format.

vbShortTime
4
Passing this value returns military time in this format hh:mm

Table 1-1 is a good reference once you’ve got a feel for how the FormatDateTime() function works or if you’re an experienced programmer. For those of you that aren’t clear on how all the information in table 1-1 relates to “real world” implementations, let’s take a look at some examples:

Returning the Current Date

If you would like to display the current date, here are a few different ways to do it along with the results they produce:

<%= FormatDateTime(Date) %> returns: 7/14/2012
(You would get the same result by coding this: <%=FormatDateTime(Date, 0) %>)

<%= FormatDateTime(Date, 1) %> returns: Friday, July 14, 2012

<%= FormatDateTime(Date, 2)%> returns: 7/14/2012

Returning the Current Time

If you would like to return the current time, here are a couple of ways to do that:

<%= FormatDateTime(Now, 3)%> returns: 6:42:31 AM

<%= FormatDateTime(Now, 4)%> returns: 06:42

Returning the Current Date and Time

If you would like to return the current date and time together, here’s how to do just that:

<%= FormatDateTime(Now) %> returns: 7/14/2012 6:42:31 AM

If you’re like me, you probably don’t like the way the date and time displays above; it’s not very cool looking, is it? In cases like this, you can actually include two FormatDateTime() functions next to each other, in order to get the date and time in a more desriptive format, like this:

<%= FormatDateTime(Date, 1) %>&nbsp;&nbsp;<%= FormatDateTime(Now, 3)%> returns:
Friday, July 14, 2012 6:42:31 AM

Adding the code to your page

Integrating the code into your ASP pages is really easy; here is how the code would look on a page with basic HTML to display the date:

<html>
<head>
<title>Here’s the date</title>
</head>

<body>

Thank you for coming to this page. The current date is: <%= FormatDateTime(Date, 1) %>

</body>

</html>

Limitations of the FormatDateTime() function

The FormatDateTime() function is an extremely handy bit of code that can help you add a touch of flair almost instantly. I would like to mention four limitations that stick out in my mind, which may be an issue to you (or your clients) depending on the project at hand:

Limitation 1: The unneeded zero

On the first through ninth days of a month the day shows up in the format of “Month 01, Year”. I know it seems like a small thing but trust me, it’s can be a big deal to some.

Limitation 2: Only basic formatting allowed

You are limited to basic formatting of the string that’s returned by the FormatDateTime() function. Since the date and/or time function returns is a single string, you can bold, italicize and change the whole date/time by adding HTML or style sheet tags around it, but you can’t change the display properties for a single part (e.g., the month).

With the FormatDateTime() function, you can do this:

<b><%= FormatDateTime(Date, 1) %></b> which would return this: Friday, July 14, 2012

But you can’t do this:

Wednesday, January 1, 2012

If limitations 1 or 2 are a major hang up for you, you’ll need to use different ASP/VBScript techniques to add the date to your page. I’ll cover those in my next article!

Limitation 3: The time isn’t necessarily “their” time.

Limitation 3 is more of a by-product than a limitation, but I figured I would keep the naming conventions the same for this section. If you use the the FormatDateTime() function (or any other date related function) on the server side, the date/time returned will be whatever the server’s date and time is, not your client’s time from their system.

If you want to ensure that the date and/or time a visitor sees on your page is the date in their part of the coutry or world, then consider using client side VBScript as an Internet Explorer only solution, or switch to client side JavaScript for a universal browser solution.

Limitation 4: It’s static.

Think of the displayed date or time as a “snap shot” of when the page was requested by the visitor. You cant use this function to display a “clock” that updates every second, or automatically update the date on the page when one day turns to the next.

If you wanted to display a dynamic clock on your page, you would need to use client side JavaScript, or VBScript (IE only) to handle that task.

I hope you’ve enjoyed this article on the FormatDateTime() function. I’ll be back soon with more date and time related ASP fun!