After learning ASP, we can build a simplest chat room based on the content we have learned. Everyone can continuously improve its functions by mastering the basic process of chat room establishment. Now let’s take a look at the methods of ASP to build a simple chat room.
The main steps are described below:
1. Add the code in the Global.asa file. This part of the code mainly processes the Application_onStart event. In this event, a data with 15 elements is defined and assigned to the properties of an Application object. The content of the Global.asa file is as follows.
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">SUB Application_OnStartdim maChats(15) Defines a data with 15 elements.Application("gaChats")=maChats Stores the content of the conversation.Application("giCounter")=0 Stores the number of existing conversations.END SUB</SCRIPT>2. Add code in the AspChat.asp file. Note that you need to add this part of the code between two horizontal lines in the homepage, that is, between two <hr> in the text. The program first determines whether the "Post" method is used when applying for this homepage, because the window submission method in this example is "post". Generally, when applying for this homepage for the first time, the "get" method is used. Therefore, if the "post" method is used, it means that a certain browser reapply when submitting his own conversation content, which needs to be processed because there is a new conversation content. Otherwise, it means that a certain browser applies for this homepage for the first time and has not submitted any conversation, so it only needs to display the current conversation content.
<p align="center"><font size=5> A simple chat room </font></p><br><%process input if Request.ServerVariables("Request_Method")="POST" then indicates the speaker if len(Request("txtWho"))>0 thenSession("ssWho")=Request("txtWho")end if blocking Application object Application.Lock creates a local reference pointer mlCounter=Application("giCounter")maChats=Application("gaChats") If the number of lines written exceeds 10, start counting again. if mlCounter>9 thenmlCounter=0end if increments user input, add 1maChat(mlChounter)=Session("ssWho")&":"&Request("txtCents")mlCounter=mlCounter+1 Sets the local variable to be valid within the application scope.Applicati "giCounter")=mlCounterApplication("gaChat")=maChat eliminates blocking of Application object Application.Unlockend if%><%Writes TextAreaif Application("giCounter")=0 thenlstemp=Application("gaChat")(0)elsefor x=0 to Application("giCounter")-1lstemp=lstemp&"<br>"Application("gaChats")(x)nextend ifResponse.white lstemp%><hr>3. Finally, initialize the content of txtWho. That is, when the browser enters his or her name once, he or she does not need to enter it again. In order to distinguish each different viewer, the "Session" object is used here. The name of the viewer is recorded using Session ("ssWho"). The following code is directly written below the above code and stored in AspChat.asp.
<form method="POST" action="aspchat.asp" name="frmAsp"><div align="center"><center><p>Speech:<input type="text" name="txtCents" size="34"></p></center></div><div align="center"><center><p>Object:<input type="submit" value="send" name="B1"></p></center></div></form>
Complete. Just add the complete htm file code before and after the 2 and 3 steps of code and save it as aspchat.asp. This is a very simple asp chat room program. You can continuously enhance its functions as you learn!
The above is how ASP builds a simple chat room. Are you also very moved? If you yearn for learning here, then take action quickly.