经过一个阶段的asp学习,下面我们结合所学过的内容建立一个最简单的聊天室,虽然很简单,但是大家可以通过他来掌握一个聊天室建立的基本过程,并且可以不断的完善其功能.
下面介绍其主要步骤:
1,添加Global.asa文件里面的代码.这部分代码主要处理Application_onStart事件,在此事件中,定义了一个有15个元素的数据,并把它赋给了一个Application对象的属性.Global.asa文件的内容如下.
| 以下为引用的内容: <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> SUB Application_OnStart dim maChats(15) 定义一个具有15个元素的数据. Application("gaChats")=maChats 存储谈话的内容. Application("giCounter")=0 存储已有的谈话数目. END SUB </SCRIPT> |
2,在AspChat.asp文件中添加代码.注意,要把这部分代码添加在主页中的两条水平线之间,也就是文本中的两个<hr>之间.程序首先判断申请这幅主页时,是否用的是"Post"方法,因为本例中窗口的提交方法是"post".而一般第一次申请这幅主页时,用的是"get"方法.所以如果采用的是"post"方法的话,就说明是某一浏览者在提交自己的谈话内容时的重新申请,这就要进行处理,因为又有新的谈话内容了.否则,说明某一浏览者第一次申请此主页,并没有提交什么谈话,因此只需要把当前的谈话内容显示出来就可以了.
| 以下为引用的内容: <p align="center"><font size=5>一个简单的聊天室</font></p> <br> <% |
| 以下为引用的内容: if Request.ServerVariables("Request_Method")="POST" then |
标明讲话者
| 以下为引用的内容: if len(Request("txtWho"))>0 then Session("ssWho")=Request("txtWho") end if |
封锁Application对象
Application.Lock
创建本地引用指针
| 以下为引用的内容: mlCounter=Application("giCounter") maChats=Application("gaChats") |
如果写入的行数超过10,则重新开始记数.
以下为引用的内容:
if mlCoundter>9 then
mlCoundter=0
end if
增加用户的输入,计数器加1
| 以下为引用的内容: maChat(mlChounter)=Session("ssWho")&":"&Request("txtCents") mlCounter=mlCounter+1 |
把局部变量设置为在应用范围内有效.
| 以下为引用的内容: Applicati "giCounter")=mlCounter Application("gaChats")=maChat |
消除Application对象的封锁
| 以下为引用的内容: Application.Unlock end if %> <% |
| 以下为引用的内容: if Application("giCounter")=0 then lstemp=Application("gaChats")(0) else for x=0 to Application("giCounter")-1 lstemp=lstemp&"<br>"Application("gaChats")(x) next end if Response.white lstemp %> <hr> |
| 以下为引用的内容: <form method="POST" action="aspchat.asp" name="frmAsp"> <div align="center"><center><p>发言:<input type="text" name="txtCents" size="34"></p> </center></div><div align="center"><center><p>对象:<input type="submit" value="送出" name="B1"></p> </center></div> </form> |

