之前发了一篇博客,是用ASP的文件读写做的聊天室,为了满足各种需求,我将会用ASP的Application来做聊天室。
首先,要让大家认识一下什么是Application
ASP的Application就是存在服务器上(不会占用服务器空间的值),它在服务器关闭时将不会被保留
先介绍一下它的语法吧:
<%
Application.Lock() '锁定Application,使其他任何脚本都不能修改该值
Application("hello") = "Good Bye World" '赋值
Application.UnLock() '解除Application的锁定
%>
<%
Response.Write(Application("hello")) '而读取的时候不需要锁定
%>
好,知道这么多后,就开始制作聊天室吧
chat.asp 最重要的页面
<%
If UCase(Trim(Request.ServerVariables("REQUEST_METHOD"))) = "POST" Then
dim sWord = Trim(Request("content"))
dim User = Trim(Request.Form("user"))
Application.Lock()
Application("chatroom") = User & ": " & sWord & " " & Application("chatroom")
'其中的“
”是在textarea中空行的符号
Application.UnLock()
End If
%>
<textarea cols="100" rows="15">
<%=Application("chatroom")%>
</textarea>
接下来是index.html
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<script src="ajax.js"></script>
<title>ASP聊天室</title>
</head>
<body onLoad="javascript:document.myform.content.focus()">
<textarea cols=100 rows=15 id="chatrecord"></textarea>
<hr>
<form method="post" action="" target="frme" name="myform">
<input name="user" type="text" placehoder="昵称"/>:<input name="content" type="text"/>
<input type="submit" value="发送"/>
</form>
<iframe src="chat.asp" name="frme"></iframe>
</body>
</html>
ajax.js
window.onload=function(){
showHint();
setInterval("showHint()", 2000);//两秒钟刷新一次
}
function showHint()//ajax
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatrecord").value = xmlhttp.responseText;
}
}
xmlhttp.open("GET","chat.asp",true);
xmlhttp.send();
}
这样就OK了!
我是用IIS来调试的
如果要配置IIS的ASP环境,请浏览:
https://blog.csdn.net/weixin_46804006/article/details/107346518