|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
ASP在国内异常流行,因为国内大多使用的是盗版的Windows和盗版的SQLServer,而ASP+COM+SQLServer实际上也是一种不错的搭配,其性能也不输于PHP+MYSQL,特别是Windows系统和SQLServer都有图形界面,比APACHE和MYSQL易于维护,因此对于不重视知识产权的国家来说也是一种不错的选择。毛病|技能WhenerrorcodinginASPit’snotasrichanenvironmentasotherenvironments.Ireallyonlyreports
thattherewasanerrorwithsomeNumbersandDescriptions.ThereisonlyafewwaysIvefoundto
reporttheseerrorsbacktotheenduser.Iveseennumerouswaysofdoingitbutfoundthiswaythe
mostgraceful.Rememberyouhavetoexplicitlycheckaftereverythingthatmightcauseanerror.The
mainonesIveexperiencesaredatabaseopeningsandrecordsetopenings&updates.Hereisthesample
codeIusetocheckforerrorsandthenredirectthemtotheerrorpageandrecordtheerrorintoa
database..Notethatallmyerrorcheckingisdonebeforethe<html>headeriswrittensoifthereisan
erroritcanredirectthepagewithoutgettinganerrorofHeadingalreadywrittentotheclient.Ifthe
htmlheaderhasbeensenttotheclientyoucantdoaresponse.redirectcommand.
Page1AsampleActiveServerPageformyouwouldusetosubmitdata
<html>
<head>
<title>Entersomedataintothefield</title>
</head>
<body>
<p>Entersomedataintothefield.Thisformisnothingmorethanrepresentinga
formyouwoulduseinreallifetosubmitsomedatatoanASPpage.Notethis
isntgoingtoenterthedataintodatabasebutitwillrecordtheerroronanErrorpage
andthenthesomeinformationabouttheError.</p>
<formmethod="POST"action="error2.asp"name="form1">
<divalign="left"><tableborder="1"width="340"height="35">
<tr>
<tdwidth="143"height="11">FavoriteComputer</td>
<tdwidth="185"height="11"><inputtype="text"name="T1"size="20"></td>
</tr>
<tr>
<tdwidth="143"height="12">FavoriteGame:</td>
<tdwidth="185"height="12"><inputtype="text"name="T2"size="20"></td>
</tr>
</table>
</div><p>:<inputtype="submit"value="Submit"name="B1"><inputtype="reset"value="Reset"
name="B2"></p>
</form>
</body>
</html>
Page2theformthatisbeingsubmittedtoandalsogeneratestheerrorthat
redirectsittotheStandardErrorPage(WhichisPage3inthisexample)
<%@Language="vbscript"%>
<%
Holdthepageinmemoryuntilresponse.flushcommandisissuedorthe</html>tagisprocessed.
Response.buffer=True
Thisforcesthepagetocontinuetoprocesseventhoughtherewasanerror.
OnErrorResumeNext
Declareallvariables
dimconn
dimrs
setconn=server.createobject("adodb.connection")
conn.open"Example_DSN"
StandardErrorcodingifthedatabasewontopenanerrornumberwillreturnsomethingelsebutzero
Ithencapturetheerrornumberanddescriptionandispassedusingthequerystringmethod
NotethedescriptionisusingtheServer.URLEncodefunction(Thiswillfillanyspacesinthe
descriptionwith
thecorrectHTMLcode
Iferr.number0Then
Response.Redirect"Error3.asp?number="&err.Number&"&desc="&Server.URLEncode(err.description)
EndIf
setrs=server.createobject("adodb.recordset")
rs.open"TableName"conn33
Explicitlycheckstoseeifthereisaproblemopeningthetable
Iferr.number0Then
Response.Redirect"Error3.asp?number="&err.Number&"&desc="&Server.URLEncode(err.description)
EndIf
rs.addnew
rs("field1")=request.form("field1")
rs("field2")=request.form("field2")
rs.update
Explicitlycheckstoseeifthereisaproblemupdatingtherecord
Iferr.number0Then
Response.Redirect"Error3.asp?number="&err.Number&"&desc="&Server.URLEncode(err.description)
EndIf
rs.close
conn.close
setrs=nothing
setconn=nothing
%>
<html>
<head>
<title>Recordsbeenadded</title></p>ActiveServerPage技术为应用开发商提供了基于脚本的直观、快速、高效的应用开发手段,极大地提高了开发的效果。在讨论ASP的安全性问题之前,让我们来看看ASP是怎么工作的。 |
|