聊天|聊天室
這次我為大家介紹的是我第一個用ASP做的程式----聊天室,很簡單,沒用資料庫,但是對於還未學到ADO的同志是個比較好的參考例子,這個例子基本包括了所以ASP的五個內建對象,但是有了聊天室的雛形,如果水平到家可以在此基礎上做的強大好看點好了,廢話不說了,現在進入正題吧!
首先這個程式有四個部分組成,分別是index.asp,chat.asp,show.asp,input.asp首先調用chat.asp然後就可以運行了!
作為一個初學者,我覺得寫一個程式整體觀念很重要,也就是說你首先要確定要實現什麼樣的功能,然後再進行各個功能的分塊和解決,那麼作為一個簡單的聊天室我覺得首先要有一個登陸介面,登陸後進入一個聊天室的主介面,主介面又分為兩個塊,上面一塊是聊天顯示的內容,下面一塊是聊天者寫入的文字並進行發送。
確定好各個功能版塊後,然後我們各個去實現!好,先解決登陸介面吧,這個容易,html隨便做個表單,我做的很簡單:
<html>
<head>
<title>歡迎光臨小型聊天室</title>
<meta http-equiv=Content-Language content=zh-cn>
</head>
<body topmargin=150 bgcolor=ffffff>
<table border=1 align="center" cellpadding=0 cellspacing=0 height=200>
<tr>
<td align=center >聊天室使用者登陸</td>
</tr>
<tr>
<td align=center height=162>
<form method=POST action="chat.asp">
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td></td>
</tr>
<tr>
<td width=37% align=center>使用者名稱</td>
<td width=63%>
<input type="text"name="userid" size=19 maxlength="10" >
</td>
</tr>
<tr>
<td >密 碼</td>
<td>
<input type="password" name="password" size=19 maxlength="30">
</td>
</tr>
<td>
<p align=center><input type=submit value="登陸" name="submit">
<input type=reset value="重寫" name=reset >
</td>
</tr>
<tr>
<td ></td>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td align=center><a href=http://www.webjx.com target="_blank">www.webjx.com</a></td>
</tr>
</table>
</body>
</html>
好了,做好了這麼個簡單的登陸頁面後下面我們進入首頁面的製作。
上面我們做了個簡單的登陸頁面,現在開始做我們的主介面chat.asp:
<%
name=request.form("userid")
password=request.form("password")
response.cookies("cname")=name
session("sname")=name
if name=""then
response.redirect"index.asp"
end if
%>
<frameset rows=80%,20%>
<frame name="show" src="show.asp">
<frame name="input" src="input.asp">
</frameset>
這個程式主要實現的是提取index.asp的表單資料,然後判斷是否使用者有填寫使用者名稱,如果沒有回到登陸介面,同時設立了cookies和session變數以用於其他檔案的調用。
做好了以後我們轉入編寫input.asp檔案,也就是使用者輸入聊天內容的部分:
<html>
<head>
<title>歡迎光臨小型聊天室</title>
</head>
<body>
<meta http-equiv=Content-Language content=zh-cn>
<form method="post" action="show.asp" target="show">
<p><font=ffff00><%=request.cookies("cname")%></font>
說:<input type="text" name=putword size="82">
<input type="submit" name=formsubmit value="發送">
<input type="reset" name=formreset value="重發">
</font>
</body>
</html>
這段代碼很簡單,有一個地方初學者可能會看不懂,target是實現將寫入的內容送到show的架構,而不是被直接在自己的架構裡!讀者可以體會一下把target去掉,接下來我們做最後一個部分也就是show.asp部分
<%
mytalk=trim(request.form("putword"))
if mytalk<>""then
application.lock
application("show")="<font color=0000ff size=4>" &
request.cookies("cname") & "</font>:" & "<font size=3>"
& mytalk & "</font>" & "<br>" & application("show")
application.unlock
end if
%>
<html>
<head>
<title>歡迎光臨小型聊天室</title>
</head>
<body>
<meta http-equiv=Content-Language content=zh-cn>
<pre>
<%=application("show")%>
</pre>
</body>
</html>
由於沒有應用程式資料庫,這段代碼主要的部分是用application來實現對聊天內容進行共用好了,很簡單的一個聊天室做好了,呵呵!不要見笑啊!