仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 894|回复: 8
打印 上一主题 下一主题

[学习教程] ASP编程:Using the Counters Object (1)

[复制链接]
灵魂腐蚀 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:48:17 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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可以完成无比强大的功能。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-16 03:26

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表