|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
无法实现跨操作系统的应用。当然这也是微软的理由之一,只有这样才能发挥ASP最佳的能力。可是我却认为正是Windows限制了ASP,ASP的概念本就是为一个能让系统运行于一个大的多样化环境而设计的;objectIntroduction:
Inyesterdaysarticle,RecordingPageHitswithMicrosoftsPageCounterObjectweexaminedusingMicrosoftsPageCounterobjecttotrackthenumberofhitsforanyparticularWebpage.Unfortunatelythiscomponentisonlybuiltforcountingpageviewhits.Ifwewantedtocountsomethingelse,sayeachtimeauserenteredaparticularsearchqueryintooursitessearchpage,wedbehardpressedwithusingthePageCounterobject.FortunatelyMicrosoftalsoprovidesdeveloperswithamoregenericcountingobject,theCountersobject.Thisobjectcanbeusedtotrackanindeterminentnumberofdeveloper-definedcounters.
BoththePageCounterandCounterscomponentsaremadeavailablewithMicrosoftsIISResourceKit.UnfortunatelythereisshoddydocumentationfortheIISResourceKit-itseemstobeisntalledonIIS5(Windows2000)bydefault,butolderversionsofIISmaynothaveitproperlyinstalled.Refertothepreviousarticle,RecordingPageHitswithMicrosoftsPageCounterObject,formoreinformationontheIISResourceKitandhowtoobtainitifyoudonthaveitinstalled.
AssumingyouhavetheIISResourceKitcomponentsproperlyinstalledyoushouldbeabletocreateaninstanceoftheCountersobjectusingthefollowingcode:
<%Setobj=Server.CreateObject("MSWC.Counters")%>
IfyougetaServer.CreateObjectFailed,Invalidclassstringerrorthenthecomponentsarenotproperlyinstalled.(IfthisisthecaseyoushouldtrychangingMSWC.CounterstoIISSample.Countersandcrossyourfingers.)Ifyoureceivenosucherrormessagethenthecomponentisproperlyinstalled.
UsingtheCountersObject:
TheCountersobjectallowsyoutocreateanynumberofpersistentcounters.WhenusingtheCountersobjectitissmartesttocreateasinlgeapplication-wideinstanceofthisobject.WecanmosteasilydothisbyaddingthefollowinglinetothetopofourGlobal.asafile:
<OBJECTRUNAT=ServerSCOPE=ApplicationID=objCounterPROGID="MSWC.Counters"></OBJECT>
TolearnmoreaboutGlobal.asabesuretoread:EverythingyouWantedtoKnowaboutGlobal.asabutwereafraidtoAskandGlobal.asaEventsaswellastheCreatingaGlobal.asaFilesectionofthesamplechapterofSamsTeachYourselfASP3.0in21Days!
OncethislineisaddedtoGlobal.asayoucanaccesstheCountersobjectfromanyASPpageinyourWebsitebytheobjectsIDthatwespecifiedintheOBJECTtag:objCounter.LikethePageCounterobjectdiscussedinyesterdaysarticle,theCountersobjectisafairlysimpleobjectconsistingofonlyfourmethods:
Get(CounterName)-RetrievesthecurrentvalueofthecounterspecifiedbythenameCounterName
Increment(CounterName)-IncrementsthecurrentvalueofthecounterspecifiedbythenameCounterName
Remove(CounterName)-RemovesthecounterspecifiedbythenameCounterNamefromthetextfilecounters.txt.Thistextfileiswherethecounterinformationispersisted.
Set(CounterName,Value)-SetsthevalueofthecounterspecifiedbythenameCounterNametothevaluespecifiedbyValue
So,oncewehaveaddedtheOBJECTtagabovetoourGlobal.asafilewecanstartworkingwiththeCountersobjectfromanyASPpageonourWebsite.WecouldusetheCountersobjectmuchlikeweusedthePageCounterobject.Forexample,ifwewantedtocreateacountertoseehowmanyfolksvisitedourhomepagewecouldusethefollowingcodeonourhomepage:
<%
Incrementthehomepagecounter
objCounter.Increment("HomePage")
Displayhowmanyhitswevehad
Response.Write"Therehavebeen"&objCounter.Get("HomePage")&"hits."
%>
ThecounternameHomePagedoesntrepresentanyspecialsortofname.Youcansimplycreateacounterbyspecifyingauniquename.Oncethecounteriscreatedyoucanincrementit,changeit,getitscurrentvalue,orremoveitfromanypageonyourWebsite.So,ifonanotherpageyouhadthesamecodeasaboveandtenvisitorshityourhomepageandthenfivevisitorshitthissecondpage,thevaluefortheHomePagecounterwillbe15-10hitsfromthehomepageand5hitsfromthesecondarypage.
InPart2wellexaminetheCountersobjectinmoredetail,lookingathowandwhereitsvaluesarepersisted.Wellalsolookatsomecodetodisplayallofthevaluesofthevariouscounterswemayhavecreated!
</p>asp可以轻松地实现对页面内容的动态控制,根据不同的浏览者,显示不同的页面内容。而浏览者一点觉察不出来,就像为他专门制作的页面一样。使用各种各样的组件,asp可以完成无比强大的功能。 |
|