By A. Keyton Weissinger
Price: $29.95 USD
£20.95 GBP
Cover | Table of Contents | Colophon
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>Sample ASP</TITLE>
</HEAD>
<BODY>
Good afternoon.<BR>
Welcome to the sample. It is now approximately
<%=Time( )%> at the server. Here are a couple of
demonstrations:<BR><BR><BR>
Some simple text formatting done using HTML:<BR>
<FONT SIZE = 1>Hello Size 1</FONT><BR>
<FONT SIZE = 2>Hello Size 2</FONT><BR>
<FONT SIZE = 3>Hello Size 3</FONT><BR>
<FONT SIZE = 4>Hello Size 4</FONT><BR>
<FONT SIZE = 5>Hello Size 5</FONT><BR>
<BR>
The same text formatting using server-side code:<BR>
<%
For intCounter = 1 to 5
%>
<FONT SIZE = <%=intCounter%>>
Hello Size <%=intCounter%></FONT><BR>
<%
Next
%>
<BR>
</BODY>
</HTML>
<HTML> <HEAD> <TITLE>Sample ASP</TITLE> </HEAD> <BODY> Good afternoon.<BR> Welcome to the sample. It is now approximately 9:28:47 at the server. Here are a couple of demonstrations:<BR><BR><BR> Some simple text formatting done using HTML:<BR> <FONT SIZE = 1>Hello Size 1</FONT><BR> <FONT SIZE = 2>Hello Size 2</FONT><BR> <FONT SIZE = 3>Hello Size 3</FONT><BR> <FONT SIZE = 4>Hello Size 4</FONT><BR> <FONT SIZE = 5>Hello Size 5</FONT><BR> <BR> The same text formatting using server-side code:<BR> <FONT SIZE = 1>Hello Size 1</FONT><BR> <FONT SIZE = 2>Hello Size 2</FONT><BR> <FONT SIZE = 3>Hello Size 3</FONT><BR> <FONT SIZE = 4>Hello Size 4</FONT><BR> <FONT SIZE = 5>Hello Size 5</FONT><BR> <BR> </BODY> </HTML>
<%
...%>
as being ASP code. (There is another way to delineate server-side code that I'll cover in
a moment.) Example 2.2 shows an active server page
named ExampleChap2.ASP, with the VBScript code
that will be interpreted by ASP.DLL in bold.<HTML>
<HEAD><TITLE>Example</TITLE></HEAD>
<BODY>
<%
' Construct a greeting string with a salutation and the
' current time on the server (retrieved from the Time( )
' function) and then display that in the HTML sent to the
' client.
strGreetingMsg = "Hello. It is now " & Time( ) & _
" on the server."
Response.Write strGreetingMsg
%>
</BODY>
</HTML>
RUNAT
attribute of the
<SCRIPT> tag. You can use the
RUNAT attribute to specify that a particular
function or subroutine is to be run (and called from) the server
side. Example 2.5 demonstrates the use of the
RUNAT attribute to create a simple function that
uses the last three letters of the domain string to return the
general type of site that it represents. This function takes a domain
string such as perl.ora.com and
returns the string "company." The
RUNAT attribute instructs ASP that this is a
server-side-only function. It will not be sent to the client and is a
valid function to call from within the server-side code. We could now
incorporate that into a script, as shown in Example 2.6.<SCRIPT LANGUAGE = "VBScript" RUNAT = SERVER>
Function DomainType(strDomainString)
strPossibleDomain = Right(strDomainString, 3)
Select Case Ucase(strPossibleDomain)
Case "COM"
DomainType = "company"
Case "EDU"
DomainType = "educational"
Case "GOV"
DomainType = "government_civil"
Case "MIL"
DomainType = "government_military"
Case Else
DomainType = "UNKNOWN"
End Select
End Function
</SCRIPT>
<HTML><HEAD><TITLE>Function Example</TITLE></HEAD>
<BODY>
<%
' In this script we'll simply initialize a string
' example parameter, but this value could have
' come from another script.
strDomainString = "perl.ora.com"
strDomainType = DomainType(strDomainString)
%>
<%=strDomainString%> is a <%=strDomainType%> site.
</BODY>
</HTML>
<SCRIPT LANGUAGE = "VBScript" RUNAT = SERVER>
Function DomainType(strDomainString)
strPossibleDomain = Right(strDomainString, 3)
Select Case Ucase(strPossibleDomain)
Case "COM"
DomainType = "company"
Case "EDU"
DomainType = "educational"
Case "GOV"
DomainType = "government_civil"
Case "MIL"
DomainType = "government_military"
Case Else
DomainType = "UNKNOWN"
End Select
End Function
</SCRIPT>@LANGUAGE
preprocessor
ASP directive. ASP directives are covered in Chapter 11. For now, know that you can use the following
line of code as the first in your script to force ASP to use JScript
as the default scripting language when interpreting your code:<%@ LANGUAGE = JScript%>
<%
' Declare local variables.
Dim objMyInfo
' Instantiate a MyInfo object.
Set objMyInfo = Server.CreateObject("MSWC.MyInfo")
' You can now initialize the values.
objMyInfo.PersonalName = "A. Keyton Weissinger"
...[additional code]
%>
<OBJECT> tag).Application.Contents.Item("Pi") = 3.14
dblMyVar = Application.Contents.Item("Pi")
dblMyVar = Application.Contents.Item(1)
Application.Contents.Remove(
Key|Index)
<%
' This script assumes you have set up two greeting salutations for all
' the members of your site based on time of day. You want to now
' remove these from your site.
strAppMorningGreeting = Application("strAMGreet")
strAppEveningGreeting = Application("strPMGreet")
.
.
.
Application.Contents.Remove("strAMGreet")
Application.Contents.Remove("strPMGreet")
.
.
.
%>
Application.Lock
<%
' This script exists on the second page of a
' multipage ASP application, so that users may
' or may not visit it. The example shows how you could
' see how many visitors the page has had.
' Assume that TotalNumPage2 starts at 0.
' Lock the Application object.
Application.Lock
intNumVisits = Application.Contents("TotalNumPage2")
intNumVisits = intNumVisits + 1
Application.Contents("TotalNumPage2") = intNumVisits
' Explicitly unlock the Application object.
Application.Unlock
' NOTE: Using the PageCnt.DLL would be a more
' efficient manner of doing this.
%>
<HTML>
<HEAD><TITLE>Home Page</TITLE></HEAD>
<BODY BGCOLOR = #ffffcc>
Welcome to our homepage. You are client number
<%= Application.Contents("TotalNumPage2")%> to our site. Thank you for your patronage.
</BODY>
</HTML>
Application_OnEnd
' <<<<<<<<<<<<<<< FROM GLOBAL.ASA >>>>>>>>>>>>>>>>>>
' This code resides in the GLOBAL.ASA file at the
' root of the current application. The following
' procedure is processed only once for the current
' application.
' See Chapter 11 for more details on the GLOBAL.ASA file.
<SCRIPT LANGUAGE="VBScript" RUNAT=Server>
Sub Application_OnEnd
' This code will run on the server when
' the application stops.
' This code saves the final count of an application
' use counter to a file.
Set filsysObj1 = _
CreateObject("Scripting.FileSystemObject")
Set tsObj1 = filsysObj1.CreateTextFile("c:\usrcount.txt", _
True)
tsObj1.WriteLine(Application.Contents("AppUserCount"))
tsObj1.Close
End Sub
</SCRIPT>
' <<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>
Dim objMyASPError Set objMyASPError = Server.GetLastError
On Error Resume Next
Dim intCounter
For intCounter = 1 to 100
intResult = intCounter / (intCounter - 100)
Next
Next statement to catch the division by zero error
that will arise when intCounter is 100.
Adding these lines to catch errors throughout your code is costly in
processing time and error-prone in its own right, since you have to
anticipate the line on which an error is likely to occur in order to
effectively trap it. As a result, and because the ASPError object
provides more information than the Err object, you should not use
On Error Resume Next if you can avoid it.objASPError.ASPCode
**** BEGIN ASPCode Example Script **** <% ' This script demonstrates the use of the ASPCode ' property of the ASPError object. Dim objMyASPError Set objMyASPError = Server.GetLastError( ) Response.Write "The value of the ASPCode property is " & objMyASPError.ASPCode %> **** END ASPCode Example Script ****
The value of the ASPCode property is [EMPTY]
The value of the ASPCode property is ASP 0128
The value of the ASPCode property is [EMPTY]
objASPError.ASPDescription<%@ LANGUAGE="VBSCRIPT" %>
<%
Response.Buffer = TRUE
Response.Expires = 0
' Script Name: 500-100_Custom.ASP
' Description: This script catches the last error and
' creates and sends a message via email
' to the responsible developer.
' Inputs: Will take whatever information was previously
' submitted into the error-raising script.
' Author: Keyton Weissinger
' Change Log:
' Date Programmer Notes
' ---------------------------------------------------
' 3/24/2000 AKW Created script.
' *** Dimension local variables needed for script. ***
' Vars for user and server info.
Dim strUser
Dim strRemoteAddress
Dim strServerName
Dim strServerAddress
' Vars for ASPError object property values.
Dim strASPCode
Dim strASPDescription
Dim strCategory
Dim strColumn
Dim strDescription
Dim strFile
Dim strLine
Dim strNumber
Dim strSource
' Vars for Request collection values.
Dim intKey
Dim strQueryStringElement
Dim strQueryStringValue
Dim strCurrentQueryString
Dim strFormElement
Dim strFormValue
Dim strCurrentForm
' Var for all HTTP header data.
Dim strAllHTTP
' Vars for Mail Message and sending.
Dim strErrMsgBody
Dim strRecipient
Dim strSender
Dim objNewMail
' Flush the buffer to ensure that no response information is sent to the
' client.
If Response.Buffer Then
Response.Clear
Response.Status = "500 Internal Server Error"
Response.ContentType = "text/html"
Response.Expires = 0
End If
' *** Initialize the user and server variables. ***
strUser = Request.ServerVariables("REMOTE_USER")
strRemoteAddress = Request.ServerVariables("REMOTE_HOST")
strServerName = Request.ServerVariables("SERVER_NAME")
strServerAddress = Request.ServerVariables("LOCAL_ADDR")
' *** Initialize the ASPError object. ***
Set objASPError = Server.GetLastError( )
' *** Initialize the ASPError object property value variables. ***
strASPCode = objASPError.ASPCode
strASPDescription = objASPError.ASPDescription
strCategory = objASPError.Category
strColumn = objASPError.Column
strDescription = objASPError.Description
strFile = objASPError.File
strLine = objASPError.Line
strNumber = objASPError.Number
strSource = objASPError.Source
' *** Retrieve the keys and values from the QueryString. ***
For Each intKey in Request.QueryString
strQueryStringElement = Request.QueryString.Key(intKey)
strQueryStringValue = Request.QueryString.Item(strQueryStringElement)
strCurrentQueryString = strCurrentQueryString & "Element: " & _
strQueryStringElement & ".......Value: " & strQueryStringValue & _
Chr(10) & Chr(13)
Next
' *** Retrieve the keys and values from the Form. ***
For Each intKey in Request.Form
strFormElement = Request.Form.Key(intKey)
strFormValue = Request.Form.Item(strFormElement)
strCurrentForm = strCurrentForm & "Element: " & _
strFormElement & ".......Value: " & strFormValue & _
Chr(10) & Chr(13)
Next
' *** Retrieve the keys and values from all HTTP headers
' sent by client. ***
strAllHTTP = Request.ServerVariables("ALL_HTTP")
' *** Construct the error email message body string. ***
strErrMsgBody = "Greetings. An error has occurred." & Chr(10) & Chr(13) & _
Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & _
"Here is information that should help you debug the script."
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13) & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "User: " & strUser & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "User Address: " & strRemoteAddress & _
Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "The error occurred on the following" _
& "server: " & vbCrLf
strErrMsgBody = strErrMsgBody & "Server Name: " & strServerName & _
vbCrLf
strErrMsgBody = strErrMsgBody & "Server I.P. Address: " & strServerAddress & _
Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "ASP Error Information:" & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "ASPCode: " & strASPCode & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "ASPDescription: " & strASPDescription & _
Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Category: " & strCategory & _
vbCrLf
strErrMsgBody = strErrMsgBody & "Column: " & strColumn & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Description: " & strDescription & _
vbCrLf
strErrMsgBody = strErrMsgBody & "File: " & strFile & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Line: " & strLine & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Number: " & strNumber & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Source: " & strSource & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13) & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Request.QueryString Information:" & _
vbCrLf
strErrMsgBody = strErrMsgBody & strCurrentQueryString & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "Request.Form Information:" & _
vbCrLf
strErrMsgBody = strErrMsgBody & strCurrentForm & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & "All HTTP headers sent by the client:" & _
Chr(10) & Chr(13)
strErrMsgBody = strErrMsgBody & strALLHTTP & Chr(10) & Chr(13)
' Initialize the NewMail object and set the mail recipient, sender and body.
' Note you may want to insert your own code here to look up the specific
' developer to which you want the error message sent.
Set objNewMail = Server.CreateObject("CDONTS.NewMail")
strRecipient = "Dev1@myapp.com"
strSender = strServerName & " at " & strServerAddress
objNewMail.Body = strErrMsgBody
objNewMail.To = strRecipient
objNewMail.From = strSender
' Send the email.
objNewMail.Send
' The rest of this script generates a non-specific error message for viewing
' by the client.
' Customize its look and feel to match that of your site.
%>
<HTML>
<HEAD>
<TITLE>
Error Notice
</TITLE>
</HEAD>
<BODY>
We apologize for the inconvenience but your last action triggered an error on
the web server.<BR>
A message has been sent to our development group and the error should be
resolved shortly.<BR>
Please contact support at 800-555-HELP for more information or try your action
again later.<BR><BR>
Thank you.
</BODY>
</HTML>