ASP编程:使用global.asp准时实行ASP
ActiveServerPage技术为应用开发商提供了基于脚本的直观、快速、高效的应用开发手段,极大地提高了开发的效果。在讨论ASP的安全性问题之前,让我们来看看ASP是怎么工作的。准时|实行Usingtheglobal.asatoscheduleASPcodeexecution.Haveyoueverhadsomeaspcodethatneededtoexecuteeveryonceinawhilebut,youjustdidntknowhowtodoit.
Thereisasolutionthatdoesntinvolverunninganyschedulingorscriptingsoftwareontheserverandisactuallyveryeasytogetworking.
Yousee...thereisthingcalledthe"global.asa".MostASPnewbiesprobablywonderwhattheheckitevenis.TheGlobal.asafileiseventdriven.Itcancontainfoureventprocedures:Application_OnStart,Application_OnEnd,Session_OnStart,andSession_OnEnd.
Theglobal.asaisbasicallyloadedintomemorythefirsttimeanyuserviewsapageonyourWebapplication.Thereareeventprocedurestubsthatcancontainscriptyouwanttobeexecutedwhentheapplicationstartsorends,orwhenthesessionstartsorends.
Withsometrickycodingyoucanusethisfiletoschedulecodetoexecute.Atleastaroundthetimeyouneeditto,thiswontbeabletomakeitexecuteatexactlyacertaintime.
Hereisthe1stexample.Itsimplykeepstrackofhowmanyvisitorshavebeentoyoursiteandafter100itresetsthecountbackto0andexecuteswhatevercodeyouneedtorun.Obviouslyyoullneedtoadjustthe"100"towhatevermakessensefortheamountofvisitorsyoursitereceives.
Contentsoftheglobal.asaarebelow.
<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubApplication_OnStart
Application("SessionCount")=0
EndSub
SubSession_OnStart
Application.Lock
Application("SessionCount")=Application("SessionCount")+1
Application.Unlock
IfApplication("SessionCount")>100Then
Application.Lock
Application("SessionCount")=0
Application.Unlock
Hereyouwouldputanycodeyouneedtorun
donotsurroundthecodewith<%%>tags
Forexampleyoumightrunadatabasequerythatchecksforexpiredaccounts
Endif
EndSub
</SCRIPT>
Nowletssayyouwantsomethingtoexecute4timesaday.Youcanstorethedate&timeinatextfileandcheckitperiodically.Whenthedateandtimegettobemorethan6hoursoldthecodewillwritethenewdate&timetothetextfileandthenexecutethecodeyouwanttorun.Youcouldchangethe"6"towhateveryouwantandthereforeexecutethecodemoreorlessoften,
Thisisaprettyslicksolutionthoughitrequirescorrectpermissionstothetextfileforreading&writing.Ifnotyoullgetanerror.
Inthisexamplewearecheckingthetextfileevery15visitors.Youcanadjustthisamountorremovethe"check"completelysothatitchecksthefileeverytime,butwhycheckthefileeverytimewhenyouhaveaverybusysite?Thatwouldjustbeawasteofserverresources,butitisuptoyouhowfaryouwanttotakethis.
Inthisexampleyouneedtostartthetextfileoffwithavaliddate&timeorelseyouwillgetanerrorbecausethescriptwillreadinanemptyvaluethe1sttime.
EXAMPLE:put6/30/996:58:45PMinthe1stlineofthetextfile.
Youcouldaddcodetocheckforthatandhandletheerror,butIdidntreallycareatthetimesoIdidntdothat.Aslongasthereisadatetherewhenitstartsitwillkeepworking.
Contentsoftheglobal.asaarebelow.
<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubApplication_OnStart
Application("SessionCount")=0
EndSub
SubSession_OnStart
Application.Lock
Application("SessionCount")=Application("SessionCount")+1
Application.Unlock
IfApplication("SessionCount")>15Then
Application.Lock
Application("SessionCount")=0
Application.Unlock
SetObjMyFile=CreateObject("Scripting.FileSystemObject")
SetOpenMyFile=ObjMyFile.OpenTextFile(Server.MapPath("last-update.txt"))
MyFileValue=OpenMyFile.ReadLine
OpenMyFile.Close
IfDateDiff("h",MyFileValue,NOW)>6Then
Hereyouwouldputanycodeyouneedtorun
donotsurroundthecodewith<%%>tags
Forexampleyoumightrunadatabasequerythatchecksforexpiredaccounts
SetWriteMyFile=ObjMyFile.CreateTextFile(Server.MapPath("last-update.txt"))
WriteMyFile.WriteLine(NOW)
WriteMyFile.Close
Endif
EndIf
EndSub
</p>ASP最大的缺点在于网络的安全性和可靠性,企业将经营数据放在开放的平台上,最大的担忧就是如何保证这些数据不被其他人破坏。 Server:这个表示的服务器,操作服务器的一些东西使用这个,如Server.Mappath转换服务器路径,Server.CreateObject实例化一个组件 封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变,业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高。 以上是语言本身的弱点,在功能方面ASP同样存在问题,第一是功能太弱,一些底层操作只能通过组件来完成,在这点上是远远比不上PHP/JSP,其次就是缺乏完善的纠错/调试功能,这点上ASP/PHP/JSP差不多。 代码逻辑混乱,难于管理:由于ASP是脚本语言混合html编程,所以你很难看清代码的逻辑关系,并且随着程序的复杂性增加,使得代码的管理十分困难,甚至超出一个程序员所能达到的管理能力,从而造成出错或这样那样的问题。 它可通过内置的组件实现更强大的功能,如使用A-DO可以轻松地访问数据库。 代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。 在平时的学习过程中要注意现学现用,注重运用,在掌握了一定的基础知识后,我们可以尝试做一些网页,也许在开始的时候我们可能会遇到很多问题,比如说如何很好的构建基本框架。 另外因为asp需要使用组件,所以了解一点组件的知识(ADODB也是组件) Request:从字面上讲就是“请求”,因此这个是处理客户端提交的东东的,例如Resuest.Form,Request.QueryString,或者干脆Request("变量名")
页:
[1]