Recommended: Useful collection of functions in ASP development (1) A collection of useful functions in ASP development is quite useful! I hope everyone keeps it! % '****************************************************** 'Prevent external submission'****************************************** function ChkPost() dim server_v1,server_v2 chkpost=false server_v1=Cstr(Request.ServerVari
In VBScript, there is an On Error Resume Next statement that causes the script interpreter to ignore runtime errors and continue execution of the script code. Then the script can check the value of the Err.Number property to determine whether an error occurred. If an error occurs, a non-zero value is returned. In ASP 3.0, you can also use On Error Goto 0 to go back to the default error handling. This kind of processing is actually performed in ASP 2.0, but there is no corresponding document description, which is common in many asp data-related processing files. Add On Error Resume Next to turn off the default error processing and then use err to catch it.If Err Then
err.Clear
An error occurred in Response.Write!
Response.End
End If
In order to get a more detailed error description, let's try the asperror object. It is a new object of asp3.0. It can be obtained through the getlasterror method of the server object . Asperror provides detailed information about the last error in asp. Unlike the Err object in VBScript, this method cannot be called at any time to see if an error occurs. It can only be used in an ASP customized error web page. If you use it by turning off the default error handling (using the On Error Resume Next statement) like using the Err object, the GetLastError method cannot access the detailed data of the error.
Properties of ASPError object:
The ASPError object provides nine attributes to indicate the nature and source of the error that occurred, and returns the actual code that caused the error. The properties and descriptions are as follows:
ASPCode: Integer. Error number generated by ASP/IIS, such as 0x800A009
ASPDescription: String type. If this error is an ASP-related error, this property is a detailed description of the error. For example: All HTTP: HTTP_ACCEPT:*/* HTTP_ACCEPT_LANGUAGE:zh-cn HTTP_CONNECTION:Keep-Alive HTTP_HOST:s HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; (R1 1.5)) ...and reports such as cookies.
Category: String type. The source of the error is that of the ASP internal scripting language, or an object.
Column: Integer. Character location in the file that generated the error
Description: String type. A brief description of the error
File: String type. The name of the file being processed when the error occurred
Line: Integer. Line number in the file that generated the error
Number : Integer. A standard COM error code
Source: String type. The actual code of the line that raised the error
OK, these are 9 properties. The syntax of using the asperror object is:
asperror.property
That's it: ASPError.ASPCode()
ASPError.ASPDescription()
ASPError.Category()
ASPError.Column()
ASPError.Description()
ASPError.File()
ASPError.Line()
ASPError.Number()
ASPError.Source()
When an ASP-related error occurs on any page under all directories supported by IIS (or in the directory where the error mapping properties are edited), the custom error page will be loaded. In fact, a normal script error trap has been set now, because the ASP runtime error on any web page in this directory will trigger a custom error page. The error web page is the default installation part of IIS and can be customized according to personal situations. For example, when we enter a non-existent web page in a directory, a 404 error occurs. When a 404 error occurs, the page used is 404b.htm. This file contains a client script code part, which obtains the URL of the current document (retrieve from the url attribute of the document object) and displays it in the page: [html]<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 3.2 Final//EN>
<html dir=ltr>
<head>
<style> a:link {font:9pt/11pt 环; color:FF0000} a: visited {font:9pt/11pt 环; color:#4e4e4e}
</style>
<META NAME=ROBOTS CONTENT=NOINDEX>
<title>Unable to find the web page</title>
<META HTTP-EQUIV=Content-Type Content=text-html; charset=gb2312>
<META NAME=MS.LOCALE CONTENT=ZH-CN>
</head>
<script>
function Homepage(){
<!--
// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm
//For testing use DocURL = res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm
DocURL = document.URL;
//this is where the http or https will be, as found by searching for :// but skipping the res://
protocolIndex=DocURL.indexOf(://,4);
//this finds the ending slash for the domain server
serverIndex=DocURL.indexOf(/,protocolIndex + 3);
//for the href, we need a valid URL to the domain. We search for the # symbol to find the beginning
//of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
//urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
BeginURL=DocURL.indexOf(#,1) + 1;
urlresult=DocURL.substring(BeginURL,serverIndex);
//for display, we need to skip after http://, and go to the next slash
displayresult=DocURL.substring(protocolIndex + 3,serverIndex);
InsertElementAnchor(urlresult, displayresult);
}
function HtmlEncode(text)
{
return text.replace(/&/g, '&').replace(/'/g, '"').replace(/</g, '<').replace(//g, '>');
}
function TagAttrib(name, value)
{
return ' '+name+'='+HtmlEncode(value)+'';
}
function PrintTag(tagName, needCloseTag, attrib, inner){
document.write( '<' + tagName + attrib + '>' + HtmlEncode(inner) );
if (needCloseTag) document.write( '</' + tagName +'>' );
}
function URI(href)
{
IEVer = window.navigator.appVersion;
IEVer = IEVer.substr( IEVer.indexOf('MSIE') + 5, 3 );
return (IEVer.charAt(1)=='.' && IEVer >= '5.5') ?
encodeURI(href):
escape(href).replace(/%3A/g, ':').replace(/%3B/g, ';');
}
function InsertElementAnchor(href, text)
{
PrintTag('A', true, TagAttrib('HREF', URI(href)), text);
}
//-->
</script>
<body bgcolor=FFFFFF>
<table width=410 cellpadding=3 cellpacing=5>
<tr>
<td align=left valign=middle width=360>
<h1 style=COLOR:000000; FONT: 12pt/15pt Songti><!--Problem-->The web page cannot be found</h1>
</td>
</tr>
<tr>
<td width=400 colspan=2> <font style=COLOR:000000; FONT: 9pt/11pt> The web page you are searching for may have been deleted, renamed, or is temporarily unavailable. </font></td>
</tr>
<tr>
<td width=400 colspan=2> <font style=COLOR:000000; FONT: 9pt/11pt Song font>
<hr color=#C0C0C0 noshade>
<p>Please try the following:</p>
<ul>
<li>If you typed a web address in the address bar, check that it is spelled correctly. <br>
</li>
<li>Open <script>
<!--
if (!((window.navigator.userAgent.indexOf(MSIE) > 0) && (window.navigator.appVersion.charAt(0) == 2)))
{
Homepage();
}
//-->
</script> Home page, find links to the required information. </li>
<li>Click the <a href=javascript:history.back(1)>Back</a> button to try other links. </li>
</ul>
<h2 style=font:9pt/11pt Songti; color:000000>HTTP 404 - File cannot be found<br> Internet Information Service<BR></h2>
<hr color=#C0C0C0 noshade>
<p>Technical Information (Supporting Individuals)</p>
<ul>
<li>Details: <br><a href=http://www.microsoft.com/ContentRedirect.asp?prd=iis&sbp=&pver=5.0&pid=&ID=404&cat=web&os=&over=&hrd=&opt1=&Opt2=&Opt3= target=_blank>Microsoft Support</a>
</li>
</ul>
</font></td>
</tr>
</table>
</body>
</html>
[/html]
When an error occurs, the mapping relationship between the error and the error web page file is determined in the Custom Errors tab of the properties dialog box of each directory. This is in the property settings in the Internet Services Manager. If you are interested, go and have a look.
Custom error The web page displays all values of the ASPError object attribute, and returns an HTTP header status message to the client by using the Response.Status method, indicating that an error has occurred. Then use the GetLastError method to get a reference to the ASPError object, so you can access the detailed data of the error:
…
<%
Response.Status = 500 Internal Server Error
Set objASPEError = Server.GetLastError()
%>
Currently executing the page: <B>show_error.asp</B><P>
<B>Error Details:</B><BR>
ASPError.ASPCode = <% = objASPEError.ASPCode %><BR>
ASPError.Number = <% = objASPEError.Number %> (0x<% = Hex(objASPEError.Number) %>)<BR>
ASPError.Source = <% = Server.HTMLEncode(objASPError.Source) %><BR>
ASPError.Category = <% = objASPEError.Category %><BR>
ASPError.File = <% = objASPEError.File %><BR>
ASPError.Line = <% = objASPEError.Line %><BR>
ASPError.Column = <% = objASPEError.Column %><BR>
ASPError.Description = <% = objASPEError.Description %><BR>
ASPError.ASPDs = <% = objASPEError.ASPDs %>
<FORM ACTION=<% = Request.ServerVariables(HTTP_REFERER) %> METHOD=POST>
<INPUT TYPE=SUBMIT NAME=cmdOK VALUE= Return to the previous page >
<P>
</FORM>If a script or ASP error occurs in a custom error page, IIS will only return a general message corresponding to the error code 500:100. This could be an error message from the script engine itself, or just a rather simple message: Internal Server Error. The customized error page will not be reloaded again.
All environments containing the wrong web page will be delivered to the customized wrong web page. That is, values stored in any ASP internal object collection or attribute can be used. For example, if you retrieve the HTTP_REFERER value from the Request.ServerVariables collection, it will reflect the URL of the page that called the original page (i.e. the page that was before the error occurred). This value does not change when the server transfers execution to the wrong webpage, and it will not contain the URL of the webpage that was executing when the error occurred.
Similarly, the SCRIPT_NAME value will be the name of the page containing the error, not the URL of the page. When an error page has been loaded, you can confirm this by checking the URL in the browser's address bar. However, the values stored in the script variables of the original web page are not available in the customized wrong web page.
If the original ASP web page is running within a transaction, that is, there is a <% @TRANSACTION=… %> directive in the front of the web page, you should also determine whether some methods need to be taken in the web page to exit the transaction. For example, you can call the SetAbort method with the built-in ObjectContext object:
objectContext.SetAbort
Well, the front-end time has sent thousands of spam emails, all of which are error-handled
Option Explicit
Response.AddHeader Status Code, 200
Response.AddHeader Reason, OK
On Error Resume Next
Response.Clear
Dim objError
Set objError = Server.GetLastError()
dim objErr, objMail, html
set objErr=Server.GetLastError()
Set objMail = CreateObject(CDONTS.NewMail)
objMail.From = [email protected]
objMail.to= [email protected]
objMail.BodyFormat = 0
objMail.MailFormat = 0
objMail.Subject = QOP Error 500
html = <font face='Verdana, Arial, Helvetica, sans-serif'><br>
html = html & <p>Error occurred at: & now
html = html & <p>Referred from: & request.ServerVariables(HTTP_REFERER)
html = html & <p>Url: & request.ServerVariables(URL)
html = html & <p><b>Category: </b></p> & objErr.Category
html = html & <p><b>Filename: </b></p> & objErr.File
html = html & <p><b>ASP Code: </b></p> & objErr.ASPCode
html = html & <p><b>Number: </b></p> & objErr.Number
html = html & <p><b>Source: </b></p> & objErr.Source
html = html & <p><b>LineNumber: </b></p> & objErr.Line
html = html & <p><b>Column: </b></p> & objErr.Column
html = html & <p><b>Description: </b></p> & objErr.Description
html = html & <p><b>ASP Description: </b></p> & objErr.ASPDescription
html = html & <blockquote>
html = html & All HTTP: & Request.ServerVariables(ALL_HTTP)
html = html & </blockquote></font>
objMail.Body = html
objMail.Send
objErr.clear
Set objMail = Nothing
Set objErr = Nothing
response.write(html)
This is really annoying to operate. Look at what Mr. Lao Gai wrote in 500-100.asp:
<%
Response.Write objASPError.Category
If objASPEError.ASPCode > Then Response.Write , & objASPEError.ASPCode
Response.Write (0x & Hex(objASPError.Number) & ) & <br>
Response.Write <b> & objASPError.Description & </b><br>
If objASPEError.ASPDs > Then Response.Write objASPEError.ASPDs & <br>
blnErrorWritten = False
' Only show the Source if it is available and the request is from the same machine as IIS
If objASPEError.Source > Then
strServername = LCase(Request.ServerVariables(SERVER_NAME))
strServerIP = Request.ServerVariables(LOCAL_ADDR)
strRemoteIP = Request.ServerVariables(REMOTE_ADDR)
If (strServername = localhost Or strServerIP = strRemoteIP) And objASPError.File <> ? Then
Response.Write objASPError.File
If objASPError.Line > 0 Then Response.Write , line & objASPError.Line
If objASPEError.Column > 0 Then Response.Write , column & objASPEError.Column
Response.Write <br>
Response.Write <font style=COLOR:000000; FONT: 8pt/11pt courier new><b>
Response.Write Server.HTMLEncode(objASPError.Source) & <br>
If objASPEError.Column > 0 Then Response.Write String((objASPEError.Column - 1), -) & ^<br>
Response.Write </b></font>
blnErrorWritten = True
End If
End If
If Not blnErrorWritten And objASPError.File <> ? Then
Response.Write <b> & objASPError.File
If objASPError.Line > 0 Then Response.Write , line & objASPError.Line
If objASPEError.Column > 0 Then Response.Write , column & objASPEError.Column
Response.Write </b><br>
End If
%>
Here is a reference: ASP 3.0 Advanced Programming Regarding the properties of using ASPError objects, there are the following points worth noting:
· Even if no error occurs, the Number property should always have a value. If no error occurs when the ASP web page calls the GetLastError method, the value of this property is 0. Normally, for the run-time error of ASP scripts, the Number property returns the hexadecimal value 0x800A0000, plus the standard script engine error code. For example, the previous example returns a value of 0x800A0009 for the Subscript out of Range error, because the error code for VBScript for this type error is 9.
· When an error has passed, the Category and Description attributes will always have a value.
· The value of the APSCode property is generated by IIS and will be empty for most script errors. In more cases, there are corresponding values when an external component is used error.
The value of the ASPDescription property is generated by the ASP preprocessor, not by the script engine currently in use, and will be empty for most script errors. More often, there is a corresponding value for errors such as calling an invalid method on an ASP built-in object.
· The File, Source, Line, and column properties can only be set when an error occurs and if the error detailed data is available. For a runtime error, the File and Line properties are usually valid, but the column property often returns -1. The Source property is returned when the error is a syntax error that prevents the page from being processed by ASP. Generally, in these cases, the Line and Column properties are valid. If you write the value of the Source property to the page, it is wise to pass the value to HTMLEncode first, in case it contains illegal HTML characters. The HTMLEncode method will be discussed in detail later in this chapter.
ERR Object
Tips: This is the second time I have written this, NND. The original focus was on the introduction of the ASPError object. I will now introduce the err object. This is a very simple and easy-to-operate object. Let's go., in the asp page.
When using an err object, you do not need to create an instance, which means you can use it casually when you want to use it. Just like the session, you do not need to create an instance like when using an ADODB object. Set conn=Server.CreateObject(ADODB.Connection) is used to create an instance. It returns an error code, but Err!=Err.Number can be cleared with the Clear method to facilitate the next use. Its main method is a Description method, which returns a brief error description. Here is a very classic example:
<%@ LANGUAGE=VBscript %>
<%Response.Buffer = True
On Error Resume Next
%>
<%
s=sa
response.write(Int(s))
If Err.Number <> 0 Then
Response.Clear
An error occurred in response.write:%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
Error Number: <%= Err.Number %><br/>
Error message: <%= Err.Description %><br/>
Error file: <%= Err.Source %><br/>
Error line: <%= Err.Line %><br/>
<%= Err %>
</BODY>
</HTML>
<%End If%>
Run it and see, Err.Line is empty, why? Because the line method written in asp's vb is not supported, this is a waste property in the support of vb.jscript, and you need to study to catch.
It is worth noting that when using the err object, On Error Resume Next must be added, and the exception that has passed the asperror object is thrown.
When linking a database, you can use the error object: Count property: used to count the number of Errors collections, Item method: used to specify a specific error, the syntax is Error.Item(number), where number is a number. Since Item is the default method, the writing method of Error(number) is equivalent to the previous one. Below is a program. Used to enumerate Error objects:
<%
On Error Resume next
Set conn=Server.CreateObject(ADODB.Connection)
Dim i, your_databasepath: your_databasepath=no.mdb
connstr=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&server.mappath(your_databasepath)&
conn.open connstr
if conn.errors.count<>0 then
response.write failed to link the database<hr/>
for i =0 to conn.errors.count-1
response.write conn.errors.item(i)&<hr>
response.write Err.Description
next
else
response.write link database successfully
end if
conn.close
%>
There is no difference between the err object, have you seen the comparison result? It’s so cold that it’s easy to use the err object directly.
It is generally recommended to use the asperror object during debugging, which means that if the On Error Resume next line is rem, it will be thrown by default with asperror. During official operation, you can use the err object to do something unless you have special requirements.
Share: Useful collection of functions in ASP development (2) A collection of useful functions in ASP development is quite useful, please keep it! '*************************************** 'Filtering hyperlink'********************************* Function checkURL(ByVal ChkStr) Dim str:str=ChkStr str=Trim(str) If IsNull(str) Then chec