This article mainly introduces ASP to build a very simple chat room. Interested friends can refer to it.
After a stage of Asp learning, we will build a simplest chat room based on the content we have learned. Although it is very simple, you can master the basic process of establishing a chat room through it and continuously improve its functions.
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 attributes of an Application object.Global.asa file The contents are as follows.
- <SCRIPTLANGUAGE=VBScriptRUNAT=Server>
- SUBApplication_OnStart
- dimmaChats(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.
- ENDSUB
- </SCRIPT>
2. Add code in the AspChat.asp file. Note that you should add this part of the code between two horizontal lines in the home page, that is, two in the text.
- <palign=center><fontsize=5> A simple chat room</font></p>
- <br>
- <%
- Process input
- ifRequest.ServerVariables(Request_Method)=POSTthen
- Sign the speaker
- iflen(Request(txtWho))>0then
- Session(ssWho)=Request(txtWho)
- endif
- Block Application Objects
- Application.Lock
- Create a local reference pointer
- mlCounter=Application(giCounter)
- maChats=Application(gaChats)
- If the number of lines written exceeds 10, start counting again.
- ifmlCounter>9then
- mlCounter=0
- endif
- Increase user input, increase counter by 1
- maChat(mlChounter)=Session(ssWho)&:&Request(txtCents)
- mlCounter=mlCounter+1
- Set local variables to be valid within application scope.
- ApplicatigiCounter)=mlCounter
- Application(gaChats)=maChat
- Eliminate Application object blocking
- Application.Unlock
- endif
- %>
- <%
- Write to TextArea
- ifApplication(giCounter)=0then
- lstemp=Application(gaChats)(0)
- else
- forx=0toApplication(giCounter)-1
- lstemp=lstemp&<br>Application(gaChats)(x)
- next
- endif
- Response.whitestemp
- %>
- <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. Use Session (ssWho) to record browsing. The following code is written directly below the above code and stored in AspChat.asp.
- <formmethod=POSTaction=aspchat.aspname=frmAsp>
- <divalign=center><center><p>Speech: <inputtype=textname=txtCentssize=34></p>
- </center></div><divalign=center><center><p>Object: <inputtype=submitvalue=send name=B1></p>
- </center></div>
- </form>
Complete. Just add the complete htm file code before and after the 2 and 3 steps and save it as aspchat.asp. This is a very simple asp chat room program. You can continuously strengthen its functions as you learn. !