日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Web入门之网络聊天室

發布時間:2023/12/20 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Web入门之网络聊天室 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

書本最后是一個聊天室的小項目,我就將之作為java web學習小階段的結束。書本上提供了完整的解決方案,實際上我參考的也不多,更多的還是自己思考設計的。過程之中發現我會的東西還是太少了,要做web后端必須也很熟悉前端才行。只能以我當前會的東西做一個簡陋的聊天室了。

先看一下界面

登陸界面

聊天界面

再說一下邏輯

數據存儲

namecontentscope
name當前用戶的名字session
usernum在線用戶數量application
username在線用戶的名字集合application
begin聊天記錄的開始位置application
end聊天記錄的結束位置application
talk11號位置application
talkxx號位置application

在application中分配50個位置來存儲聊天記錄,分別為1-50,這相當于一個隊列,FIFO,begin和end來記錄隊列的起始和結束。

流程

login.html,輸入名字→check.jsp,將名字寫入session和application→charroom.jsp,聊天界面。
聊天界面由3個frame構成,users.jsp負責顯示在線用戶列表,input.jsp負責用戶輸入信息,chats.jsp負責顯示所有聊天記錄。
input.jsp將用戶輸入的內容post給talk.jsp,talk.jsp負責將這些內容存到application中。

代碼

login.html,登陸界面

<html><meta http-equiv="content-type" content="text/html;charset=utf-8"> <script type="text/javascript">// 從url 字符串中提取變量的值function GetQueryValue(sorStr, panStr){var vStr = "";if (sorStr==null || sorStr=="" || panStr==null || panStr=="") return vStr;sorStr = sorStr.toLowerCase();panStr += "=";var itmp = sorStr.indexOf(panStr);if (itmp < 0) return vStr;sorStr = sorStr.substr(itmp + panStr.length);itmp = sorStr.indexOf("&");if (itmp < 0) return sorStr;else{sorStr = sorStr.substr(0, itmp);return sorStr;}} </script><body><br><br><br><br><center> 大俠請輸入你的名號<br><form action="check.jsp" method="post"><input type=text name="name"><input type=submit value="開始"></form> <p id="alert"></p> <script> // 獲得url字符串 var strGetQuery = document.location.search; // 獲得alert參數的值 var stylevalue = GetQueryValue(strGetQuery,'alert'); // 輸出 document.getElementById("alert").innerHTML = stylevalue; </script> </center></body> </html>

它可以接受alert參數,并將參數內容輸出。

check.jsp,用來檢查名字有效性

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <html> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <body><%request.setCharacterEncoding("utf-8");//名字為空String name = request.getParameter("name");if(name == null || name == "") {response.sendRedirect("login.html");return;}//這是第一個用戶String strusernum = (String)application.getAttribute("usernum");int usernum = 0;if(strusernum != null) usernum = Integer.parseInt(strusernum);if(strusernum == null || usernum == 0) {application.setAttribute("username", name+";");application.setAttribute("usernum", "1");session.setAttribute("name", name);response.sendRedirect("chatroom.jsp");return;}//已有用戶String username = (String)application.getAttribute("username");if(username.contains(name)) {response.sendRedirect("login.html?alert=The_name_already_exists.");return;}//新用戶++usernum;application.setAttribute("username", username+name+";");application.setAttribute("usernum", ""+usernum);session.setAttribute("name", name);response.sendRedirect("chatroom.jsp");%> </body> </html>

chatroom.jsp,這是聊天室主界面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><title>chat room</title></head><%response.setHeader("refresh","5");%><%if(request.getSession().getAttribute("name") == null){%><jsp:forward page="login.html"/><%}%><frameset cols="80%,*"><frameset rows="70%,*" ><frame src="chats.jsp" name="frachats" scrolling="no"><frame src="input.jsp" scrolling="no"></frameset><frame src="users.jsp" frameborder=0></frameset> </html>

users.jsp,顯示當前在線用戶

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <html><meta http-equiv="content-type" content="text/html;charset=utf-8"> <body> <%String strusernum = (String)application.getAttribute("usernum");%><%=strusernum%>位在線用戶:<hr><%int usernum = Integer.parseInt(strusernum);String username = (String)application.getAttribute("username");String[] names = username.split(";");for (int i=0; i<usernum; ++i) out.print(names[i]+"<br>");%> </body> </html>

input.jsp,用戶輸入消息的界面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <html><meta http-equiv="content-type" content="text/html;charset=utf-8"> <head><script language="javascript">function chk(){if(content.txttalk.valu == "") return;content.submit();content.txttalk.value = "";}</script> </head> <body><p>你好,<%=(String)request.getSession().getAttribute("name")%></p><form name=content action="talk.jsp" method="post" target="frachats"><textarea name="txttalk" rows="4" cols="100"></textarea><input type="button" value="發言" onclick="chk()"><input name="" type="reset" value="清除"></form> </body> </html>

chats.jsp,顯示所有聊天記錄的界面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <html><meta http-equiv="content-type" content="text/html;charset=utf-8"><script type="text/javascript">function pagescrolltoend() {var c = window.document.body.scrollHeight;window.scroll(0,c); }function divscrolltoend() {var div = document.getElementById('scrolldIV');div.scrollTop = div.scrollHeight; }</script> <body onload="divscrolltoend()"><marquee>通知:本聊天室正式開放</marquee><br><br><div id="scrolldIV" style="overflow:scroll; height: 500px; width: auto; border: 1px solid #999;"><%String strbegin = (String)application.getAttribute("begin");if(strbegin == null){out.println("nothing");return;} else {int begin = Integer.parseInt(strbegin);int end = Integer.parseInt((String)application.getAttribute("end"));if (begin < end) {for (int i=begin; i<end; ++i) {out.print((String)application.getAttribute("talk"+i)+"<br>");}}else {for(int i=begin; i<102; ++i) out.println((String)application.getAttribute("talk"+i)+"<br>");for(int i=1; i<end; ++i) out.println((String)application.getAttribute("talk"+i)+"<br>");}}%></div> </body> </html>

talk.jsp,處理用戶消息,將其插入消息記錄的頁面

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <html><meta http-equiv="content-type" content="text/html;charset=utf-8"> <body><%request.setCharacterEncoding("utf-8");String text = request.getParameter("txttalk");if(text == null || text == "") {response.sendRedirect("chats.jsp");return;}String name = (String)session.getAttribute("name");Date date=new Date();SimpleDateFormat f=new SimpleDateFormat(" [HH:mm:ss]");String time=f.format(date);String strbegin = (String)application.getAttribute("begin");text = name+time+"<br>"+text;if(strbegin == null) {application.setAttribute("begin", "1");application.setAttribute("end", "2");application.setAttribute("talk1", text);} else {String strend = (String)application.getAttribute("end");int end = Integer.parseInt(strend);int begin = Integer.parseInt(strbegin);application.setAttribute("talk"+end, text);++end;if(end == 102) end = 1;if(end == begin) {++begin;if(begin ==102) begin = 1;application.setAttribute("begin", ""+begin);}application.setAttribute("end", ""+end);}response.sendRedirect("chats.jsp");%> </body> </html>

bugs

信息的實時顯示,通過設定頁面自動更新response.setHeader("refresh","5");來實現,沒辦法,前端的東西不懂,只能這樣實現了。
重要問題!用戶關閉瀏覽器或因其他原因退出聊天室的時候,服務器是沒響應的!預期解決方法:將session的有效期設置為一個較小的值,并為session的銷毀設定一個監聽器,session銷毀時,將application中存儲的此用戶刪去。
關于session和監聽器的內容,后面的文章再說。在線用戶刪除功能就不實現了吧。

總結

以上是生活随笔為你收集整理的Java Web入门之网络聊天室的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。