山那边是海 发表于 2015-1-16 22:28:00

ASP网页编程之用多种办法制造WEB页面的计数器

SQLServer是基于服务器端的中型的数据库,可以适合大容量数据的应用,在功能上管理上也要比Access要强得多。在处理海量数据的效率,后台开发的灵活性,可扩展性等方面强大。Onewaytodoit:

Doyouliketoknowhowmanyusersvisitedyoursite?CreatingaWebcounterisveryeasythingtodo
usingASP.Theonlythingyouhavetodoistouseanapplicationservervariablecalledcount,this
variablewillhavethevaluezerowhenyourapplicationisstartedforthefirsttime,andeverytimeanew
visitorwillcometoyoursitethecountvariablewillbeincremented,theincrementsectionwillbe
writtenintheSession_OnStartintheGlobal.asathroughthreesteps:
¨Locktheapplicationserversonomorethanonevisitorwilltrytoincreaseitatthesametime.
¨Incrementthecountvariablebyone.
¨Unlockthevariable.
Andthat’sallwhatyouhavetodotocreateasimplecounterandhereisthecode.
FirstletusdotheGlobal.asasection:

<SCRIPTLANGUAGE=VBScriptRUNAT=Server>
SubSession_OnStart
LocktheApplicationobject
Application.Lock

Incrementthecount
Application("count")=Application("count")+1

UnlocktheApplicationobject
Application.Unlock
EndSub
</SCRIPT>


Andnowtoshowtheresultyoujustneedtoretrievethecountvariablelikethisinyourwebpage:
<%@language="vbscript"%>
<HTML>
<HEAD>
<TITLE>CounterExample1</TITLE>
</HEAD>
<BODY>
<H1>Youarethevisitornumber<%=Application("count")%></H1>
</BODY>
</HTML>



Anotherwaytodoit:

Okthat’sverynice,butwhatwillhappeniftheapplicationstopsforanyreason?Youwillloseallthe
datastoredintheapplicationvariablesandthatincludesthecountvariable.TosolvethisproblemI
willuseanotherway,youneedtostorethevalueofthecountvariablewithinatextfileordatabase.
FormeIpreferdatabasesoIwilluseanMSaccessdatabasewithatableofonefieldcalledcountand
thefieldcalledcounteroftypeNUMBER.

<%
Declarethevariablesthatwillbeusedwithourcode
DimConnection,RS,ConStr,Counts

Createandopenthedatabaseconnection
SetConnection=Server.Createobject("ADODB.Connection")
ConStr=("DRIVER={MicrosoftAccessDriver(*.mdb)};DBQ="&Server.MapPath("counter.mdb"))
Connection.OpenConStr

Createtherecordsetandretrivethecountervalue
SetRS=Connection.Execute("SELECT*FROMcount")

Increasethecountervaluebyone
Counts=RS("counter")+1

Updatethecountervalueinthedatabase
SetRS=Connection.Execute("UPDATEcountSETcounter="&Counts)
Connection.Close
%>

<HTML>
<HEAD>
<TITLE>CounterExample2</TITLE>
</HEAD>
<BODY>
<H1>Youarethevisitornumber<%=Counts%></H1>
</BODY>
</HTML>



WhataboutActiveusers:

Someguyswantstoknowhowmanyvisitorsarecurrentlyseeingthesite,forthatwewilluseanotherway,
wewillcreateanapplicationvariablecalledactive,andoneachnewsessionwewillincreaseitbyone
andwhenasessionendswewilldecreaseitbyone,andhereisthecode:

TheGlobal.asa:
<SCRIPTLANGUAGE="VBScript"RUNAT="Server">

SubApplication_OnStart
Createthevariablethatwillholdthenumberofactivevisitors
Application("Active")=0
EndSub

SubSession_OnStart
Increasethecounterbyone
Application.Lock
Application("Active")=Application("Active")+1
Application.UnLock

EndSub

SubSession_OnEnd
Decreasethecounter
Application.Lock
Application("Active")=Application("Active")-1
Application.UnLock
EndSub

</SCRIPT>


AndtoshowtheresultsjustcalltheApplicationvariableactiveinyourwebpagejustlikethis:
<%=Application("Active")%>




Ihopeyoucancreateyourowncountersfromnowon.Giveittryandgoodluck.
专业性的服务。有的ASP商提供垂直型的应用服务,针对某一特定行业提供应用服务。

若相依 发表于 2015-1-19 13:35:44

用户端的浏览器不需要提供任何别的支持,这样大提高了用户与服务器之间的交互的速度。

莫相离 发表于 2015-1-24 16:42:22

Session:这个存储跟客户端会话过程的数据,默认20分钟失效

小妖女 发表于 2015-2-2 11:11:35

它可通过内置的组件实现更强大的功能,如使用A-DO可以轻松地访问数据库。

谁可相欹 发表于 2015-2-7 18:40:24

你可以通过继承已有的对象最大限度保护你以前的投资。并且C#和C++、Java一样提供了完善的调试/纠错体系。

透明 发表于 2015-2-22 22:57:45

不能只是将它停留在纸上谈兵的程度上。

admin 发表于 2015-3-7 04:15:00

哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的?

冷月葬花魂 发表于 2015-3-14 11:46:35

在平时的学习过程中要注意现学现用,注重运用,在掌握了一定的基础知识后,我们可以尝试做一些网页,也许在开始的时候我们可能会遇到很多问题,比如说如何很好的构建基本框架。
页: [1]
查看完整版本: ASP网页编程之用多种办法制造WEB页面的计数器